2D arraylist
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Bob_Garnis
Posted On:   Tuesday, October 21, 2003 05:23 PM

I am not sure how to make a 2D arraylist? Any help will be useful.


Another question along the same lines, as far as, using a nested "dot" operation on a collection. an example would be:


ArrayList al = new ArrayList();
ArrayList a2 = new ArrayList();


a2.add(a1);


How is it possible to use the dot operations on a1 through a2? a2.get(0).size() suppose to get size of a1?
Please use html tags to format code blocks.

Re: 2D arraylist

Posted By:   Aravind_Thangaraj  
Posted On:   Monday, December 1, 2003 09:27 PM


Why do you not try vectors to create 2d arrays...?

Try this code snipset...

Vector Row = new vector();
// this is main vector for rows
// whenever you want to add an row, just create a new vector and add it with the vector Row.
{
Vector row1 = new Vector();
}
// if you want to add a column in an i'th row,
// point to that vector object and in that object add the data.

Row(i).add(data); // may not be exact in syntax. check it.

You can add any count of rows and columns using this.
It is very dynamic.

Try out and let me know about the result.

good day.

regards,
aravind. t

Re: 2D arraylist

Posted By:   Stephen_McConnell  
Posted On:   Wednesday, October 22, 2003 05:28 AM

You are starting out right. Just the devil is in the details.


You do want to nest array lists. But since the return of a "get(int index)" is an Object, you have to cast it to an ArrayList before you can access the arraylist. Here's how to do it.


((ArrayList)a2.get(index)).size();

Note that I casted "a2" as an ArrayList, then wrapped the entire thing in parens... then accessed the method on it.


The compiler (and most IDE's like Eclipse, JBuilder, Forte or NetBeans will recognize this structure for code completion) is smart enough to figure out what you are wanting to do.


Good luck


Stephen Mcconnell

About | Sitemap | Contact