-
Array Lists
If I declare 2 Array Lists, and add the second one to the first, how can I access the second one through the first. This probably dont make any sense to anybody so I'll also try it this way...
PHP Code:
void Function()
{
ArrayList List1 = new ArrayList();
ArrayList List2 = new ArrayList();
for (int x = 0; x < 10; x++)
List2.Add(x); // Fill the List with integers
List1.Add(List2); // Add List2 to List1
// Now, say I want to access the 2nd item in List2 through List1...
int y = (int)List1[0][1]; // <-- Wont compile
// However, I can create a temporary array list and get it that way.
ArrayList tmpList = new ArrayList();
tmpList = (ArrayList)List1[0]; // List1[0] is List2
// Now I can access the individual items
int y = (int)tmpList[0];
}
Do you see what I'm trying to do?
I'm trying to access elements of List2 by going through List1. I dont want to create a temporary ArrayList and copy to it... that seems like a waste of resources.
-
Re: Array Lists
Either:
this line:
Code:
int y = (int)List1[0][1];
becomes this:
Code:
int y = (int)((ArrayList)List1[0])[1];
or these lines:
Code:
ArrayList tmpList = new ArrayList();
tmpList = (ArrayList)List1[0];
become this:
Code:
ArrayList tmpList = (ArrayList)List1[0];
-
Re: Array Lists
Yes, as jmcilhinney demonstrated you need to cast the item back to an ArrayList because it stores generic objects.
-
Re: Array Lists
Thanks guys... Just what I needed! :)