471,313 Members | 2,096 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,313 software developers and data experts.

Arraylist of Arraylist

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?

Jan 25 '06 #1
6 1718
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[i]).ToArray(typeof(int);

(ArrayList)mainAL[i] 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
Jan 25 '06 #2
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.
"Iapain" <ia****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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?

Jan 26 '06 #3

"Iapain" <ia****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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]
Jan 26 '06 #4
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?

Jan 26 '06 #5
Iapain wrote:
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

Jan 26 '06 #6
Absolutly correct Jon, thank you guys! i've solved my problem.

Jan 26 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Alex Ting | last post: by
3 posts views Thread by george r smith | last post: by
6 posts views Thread by gane kol | last post: by
4 posts views Thread by Peter | last post: by
18 posts views Thread by Sam | last post: by
6 posts views Thread by fniles | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.