Multi-Dimensional Arraylist

G

Guest

How to I populate and reference multi-dimensional ArrayList.

I would like to populate an Array list with the Add method something like so:

ArrayList a = new ArrayList;
ArrayList b = new ArrayList;
a.Add = "A";
a.Add = "B";
a.Add = "C";
b.Add = a;
a.Clear();
a.Add = "x";
a.Add = 'y';
b.Add = b;

Then turn around and go through it with two for loops and print out each
string from ArrayList A, for the two different ArrayList elements with a
MessageBox. Would someone know how to do this?
 
M

Morten Wennevik

Hi Greg,

You need to create a new instance of ArrayList instead of clearing the one
you already filled.

ArrayList a = new ArrayList;
ArrayList b = new ArrayList;
a.Add = "A";
a.Add = "B";
a.Add = "C";
b.Add = a;
a = new ArrayList();
a.Add = "x";
a.Add = 'y';
b.Add = a;

....

ArrayList a1 = (ArrayList)b[0];
ArrayList a2 = (ArrayList)b[1];
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top