Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multidimensional ArrayList

 
Chris Cairns
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the syntax for creating a multidimensional ArrayList?
 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

create an array list, then add an array list to it as the object. to add an object in to the matrix, you have to get the array list at that row, type cast it back to an ArrayList (because array list only holds type Object), then add something to that array list. to access the objects held by the matrix, you get the appropriate row, type cast that row to an ArrayList, then get the appropriate column and type cast that column back to the appropriate object (in this case String).
also note that the matrix doesn't have to be square - it can have rows of differing lengths.
[ April 02, 2003: Message edited by: Jon Dornback ]
 
corrado martini
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Dornback wrote:
create an array list, then add an array list to it as the object. to add an object in to the matrix, you have to get the array list at that row, type cast it back to an ArrayList (because array list only holds type Object), then add something to that array list. to access the objects held by the matrix, you get the appropriate row, type cast that row to an ArrayList, then get the appropriate column and type cast that column back to the appropriate object (in this case String).
also note that the matrix doesn't have to be square - it can have rows of differing lengths.
[ April 02, 2003: Message edited by: Jon Dornback ]

Many thanks, Java Ranch is the greatest
 
Andreas Hollmann
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want create a matrix I advice ArrayTable of google guava libraries.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic