Query on ArrayList - How to access
Hi
I´d like to know how to access the data from an arrayList which is inside another arrayList.
Code:
//I create two arraylists.
ArrayList myArray1 = new ArrayList();
ArrayList myArray2 = new ArrayList();
//Add data to myArray1
myArray1.Add("Array1string1");
myArray1.Add("Array1string2");
//Add data to myArray2.
myArray2.Add("Array2String1");
//Add myArrayList2 to myArrayList1
myArray1.Add(myArray2);
My question is , how could I print on screen or access the data from the second array, as per:
Accessing data from index 0 and 1.
Code:
myArray1[0].ToString() +"\n";
myArray1[1].ToString();
How would I access data from index 2 ? Which is in this case an arraylist.
Thanks!
Re: Query on ArrayList - How to access
Quote:
Originally Posted by Rauland
Hi
I´d like to know how to access the data from an arrayList which is inside another arrayList.
Code:
//I create two arraylists.
ArrayList myArray1 = new ArrayList();
ArrayList myArray2 = new ArrayList();
//Add data to myArray1
myArray1.Add("Array1string1");
myArray1.Add("Array1string2");
//Add data to myArray2.
myArray2.Add("Array2String1");
//Add myArrayList2 to myArrayList1
myArray1.Add(myArray2);
My question is , how could I print on screen or access the data from the second array, as per:
Accessing data from index 0 and 1.
Code:
myArray1[0].ToString() +"\n";
myArray1[1].ToString();
How would I access data from index 2 ? Which is in this case an arraylist.
Thanks!
same as you access as you give in your sample. just put 2 inside the bracket.
ex. myArray1[2].ToString();
Re: Query on ArrayList - How to access
Sorry I post a wrong answer
ArrayList test = (ArrayList)al1[2];
System.Console.WriteLine(test[0]);
or you could do these one
System.Console.WriteLine(((ArrayList)al1[2])[0]);
Re: Query on ArrayList - How to access
You haven't said what .NET version you're using. There are buttons for this on the New Thread page; please use them in future so that we avoid any confusion and wasted time.
If you're using .NET 2.0, you should use generic Lists, then you can avoid the casting mess.
Re: Query on ArrayList - How to access
Quote:
Originally Posted by popskie
Sorry I post a wrong answer
ArrayList test = (ArrayList)al1[2];
System.Console.WriteLine(test[0]);
or you could do these one
System.Console.WriteLine(((ArrayList)al1[2])[0]);
Thanks!
Re: Query on ArrayList - How to access
Quote:
Originally Posted by penagate
You haven't said what .NET version you're using. There are buttons for this on the New Thread page; please use them in future so that we avoid any confusion and wasted time.
If you're using .NET 2.0, you should use generic Lists, then you can avoid the casting mess.
I am using .NET 2.0, How would you go about this using generic Lists?
Cheers!
Re: Query on ArrayList - How to access
List<string> or List<List<string>>. However, you can't have different types. What you are doing is a bad practice. If your goal is to create some sort of tree structure, make a class to represent each node, which can contain children. Otherwise, if you just want a fixed-depth list of lists of strings, use List<List<string>>.
Re: Query on ArrayList - How to access
Quote:
Originally Posted by Rauland
I am using .NET 2.0, How would you go about this using generic Lists?
Cheers!
As what Penagate said if your using 2.0 then can do this
Code:
List<List<string>> l1 = new List<List<string>>();
List<string> l2 = new List<string>();
l2.Add("Your Data");
l1.Add(l2);
System.Console.WriteLine((l1[0])[0]);
Re: Query on ArrayList - How to access
Re: Query on ArrayList - How to access
Quote:
Originally Posted by penagate
Why not just l1[0][0].
OK LOL