|
-
Mar 13th, 2007, 02:51 AM
#1
Thread Starter
Hyperactive Member
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!
-
Mar 13th, 2007, 02:55 AM
#2
Fanatic Member
Re: Query on ArrayList - How to access
 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();
Last edited by popskie; Mar 13th, 2007 at 02:58 AM.
-
Mar 13th, 2007, 03:09 AM
#3
Fanatic Member
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]);
Last edited by popskie; Mar 13th, 2007 at 03:13 AM.
-
Mar 13th, 2007, 03:25 AM
#4
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.
-
Mar 13th, 2007, 03:46 AM
#5
Thread Starter
Hyperactive Member
Re: Query on ArrayList - How to access
 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!
-
Mar 13th, 2007, 03:48 AM
#6
Thread Starter
Hyperactive Member
Re: Query on ArrayList - How to access
 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!
-
Mar 13th, 2007, 04:12 AM
#7
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>>.
-
Mar 13th, 2007, 04:52 AM
#8
Fanatic Member
Re: Query on ArrayList - How to access
 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]);
-
Mar 13th, 2007, 04:59 AM
#9
Re: Query on ArrayList - How to access
-
Mar 13th, 2007, 05:02 AM
#10
Fanatic Member
Re: Query on ArrayList - How to access
 Originally Posted by penagate
Why not just l1[0][0].
OK LOL
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|