Results 1 to 10 of 10

Thread: Query on ArrayList - How to access

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    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!

  2. #2
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    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();

  3. #3
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    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]);

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    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!

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    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!

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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>>.

  8. #8
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    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]);

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Query on ArrayList - How to access

    Why not just l1[0][0].

  10. #10
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Re: Query on ArrayList - How to access

    Quote 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
  •  



Click Here to Expand Forum to Full Width