Results 1 to 4 of 4

Thread: Array Lists

  1. #1

    Thread Starter
    Lively Member DanielS1324's Avatar
    Join Date
    Apr 2005
    Posts
    79

    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 010x++)
              
    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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];
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    Re: Array Lists

    Yes, as jmcilhinney demonstrated you need to cast the item back to an ArrayList because it stores generic objects.

  4. #4

    Thread Starter
    Lively Member DanielS1324's Avatar
    Join Date
    Apr 2005
    Posts
    79

    Re: Array Lists

    Thanks guys... Just what I needed!

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