By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,587 Members | 1,168 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,587 IT Pros & Developers. It's quick & easy.

Arraylist of Arraylist

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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.