Arraylist of Arraylist

I

Iapain

Hello,

I've created an arraylist which consist arraylist ..like

ArrayList myAL = new ArrayList(); // Main Array List
ArrayList tmpAL = new ArrayList(); //Temp Array List
tmpAL.Add(2); //Add int to Temp AL
tmpAL.Add(3); //Add int to Temp AL
myAL.Add(tmpAL); //Add Temp AL to Main AL
//(and so on)
..
int[] data = (int[])myAL[0].ToArray(typeof(int); // something like this
...i have to access the element in Main arraylist whicj are itself an
arraylist and then convert them into int[].

now i have to convert myAL to int[] .. problem is in converting object
to Array, any hints?
 
R

Rudderius

Iapain,
now i have to convert myAL to int[] .. problem is in converting object
to Array, any hints?

you have to cast the object you get from the main arraylist, which is an
arraylist, as an arraylist which can be converted to an array

it sounds hard but is is as simple as this:

(int[])((ArrayList)mainAL).ToArray(typeof(int);

(ArrayList)mainAL means: get the i-th element from main array and
cast it to an ArrayList, the result is the i-th arraylist in your mainAL.

hope this will be clare enough
 
K

Kai Brinkmann [MSFT]

ArrayList temp = (ArrayList)myAL[0];
int[] data = new int[temp.Count]
temp.CopyTo(data);

Ths last statement throws an InvalidCastException in case the elements in
the source ArrayList cannot be cast to the type of the destination array.

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
 
N

Nick Hounsome

Iapain said:
Hello,

I've created an arraylist which consist arraylist ..like

ArrayList myAL = new ArrayList(); // Main Array List
ArrayList tmpAL = new ArrayList(); //Temp Array List
tmpAL.Add(2); //Add int to Temp AL
tmpAL.Add(3); //Add int to Temp AL
myAL.Add(tmpAL); //Add Temp AL to Main AL
//(and so on)
.
int[] data = (int[])myAL[0].ToArray(typeof(int); // something like this
..i have to access the element in Main arraylist whicj are itself an
arraylist and then convert them into int[].

now i have to convert myAL to int[] .. problem is in converting object
to Array, any hints?

Apart from all the other stuff that people have responded you should never
right ugly stuff like this.

The way to go is to write your own matrix class.
The underlying implementation can use ArrayList but by using your own class
you will acheive 3 benefits:

1) It's easier for other to use and understand.
2) It can be typesafe
3) It helps you to think more clearly about the types involved than trying
to stuff it all into a single complicated expression with a lot of casting.
[For example the casting to ArrayList would have been obvious]
 
I

Iapain

Both Rudderius and Kai suggested an excellent way to use Arraylist of
Arraylist, Nick i your suggestion is excellent too ..Thanks it
certainly helped me a lot. I've one more question may be its stupid but
its happening with me .. If i clear tempAL like below

ArrayList myAL = new ArrayList(); // Main Array List
ArrayList tmpAL = new ArrayList(); //Temp Array List
tmpAL.Add(2); //Add int to Temp AL
tmpAL.Add(3); //Add int to Temp AL
myAL.Add(tmpAL); //Add Temp AL to Main AL
tmpAL.Clear() // clear temp AL

now i am clearing the tmpAL after adding it to MainAL ..would the
content of MainAL have tmpAL? I've tried and their is no content in
MainAL ?? any solution?
 
J

Jon Skeet [C# MVP]

Iapain said:
Both Rudderius and Kai suggested an excellent way to use Arraylist of
Arraylist, Nick i your suggestion is excellent too ..Thanks it
certainly helped me a lot. I've one more question may be its stupid but
its happening with me .. If i clear tempAL like below

ArrayList myAL = new ArrayList(); // Main Array List
ArrayList tmpAL = new ArrayList(); //Temp Array List
tmpAL.Add(2); //Add int to Temp AL
tmpAL.Add(3); //Add int to Temp AL
myAL.Add(tmpAL); //Add Temp AL to Main AL
tmpAL.Clear() // clear temp AL

now i am clearing the tmpAL after adding it to MainAL ..would the
content of MainAL have tmpAL? I've tried and their is no content in
MainAL ?? any solution?

You need to be aware of reference type semantics. ArrayLists store
references to objects, so whatever you do to tmpAL (while it's still
referring to the same object) is visible via myAL[0], because they're
both references to the same object. To put this in a slightly simpler
context (with only one ArrayList involved) if you do:

ArrayList list1 = new ArrayList();
ArrayList list2 = list1;

list2.Add("Hello");
Console.WriteLine (list1.Count);

the output will be 1, because the values of list1 and list2 are
references to the same object.

Jon
 

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