Accessing Different "Levels" of ArrayList

This is a discussion on Accessing Different "Levels" of ArrayList within the C# Programming forums, part of the General Programming Boards category; Hi, Can anyone help me with the syntax to access different levels of an ArrayList? Something like: Code: rawData[0] [0] ...

  1. #1
    Registered User stevespai's Avatar
    Join Date
    Jun 2006
    Posts
    23

    Accessing Different "Levels" of ArrayList

    Hi,

    Can anyone help me with the syntax to access different levels of an ArrayList?

    Something like:

    Code:
    rawData[0]          [0]    [0]
                               [1]
                        [1]    [0]
                               [1]
                               [2]
                               [3]
                        [2]    [0]
                               [1]
                               [2]
                        [3]    [0]
                               [1]
                               [2]
                               [3]
                               [4]
    where each column to the right is another "Level"

    Thanks.
    Steve
    Last edited by stevespai; 07-31-2006 at 03:54 PM.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    If it's a multi-dimensional array, you access members as list[0,2,1]. If it's a jagged array, you access members as list[0][2][1].
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >If it's a multi-dimensional array
    It's an ArrayList object.

    >If it's a jagged array
    It's an ArrayList object. Neither of those solutions will work unless he wants to call the ToArray method, which is exceptionally wasteful.

    >Can anyone help me with the syntax to access different levels of an ArrayList?
    Something like this?
    Code:
    namespace jsw {
        using System;
        using System.Collections;
    
        public class Test {
            public static void Main()
            {
                ArrayList al = new ArrayList();
    
                // Build a 5x5 ArrayList
                for (int i = 0; i < 5; i++) {
                    al.Add(new ArrayList());
    
                    for (int j = 0; j < 5; j++)
                        ((ArrayList)al[i]).Add(i * j + 1);
                }
    
                // Display the table
                foreach (ArrayList row in al) {
                    foreach (int item in row)
                        Console.Write("[{0,2}]", item);
                    Console.WriteLine();
                }
    
                // Get the item at index [2][3]
                int item2 = (int)((ArrayList)al[2])[3];
    
                Console.WriteLine("At index [2][3]: {0}", item2);
            }
        }
    }
    My best code is written with the delete key.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    An ArrayList of ArrayLists is conceptually the same as a jagged array, and you're indexing it exactly the same as I said you should, with the exception of casting. I can see that it could be confusing trying to index into that sort of structure since you have to cast all over the place. If you use generic lists instead of ArrayList, you don't have that problem.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >An ArrayList of ArrayLists is conceptually the same as a jagged array
    Conceptually, yes. But your suggestion failed to mention that you were talking in the abstract.
    My best code is written with the delete key.

  6. #6
    Registered User stevespai's Avatar
    Join Date
    Jun 2006
    Posts
    23
    Thank you both for the replies, let me read over the method and try to understand it.

    pianorain: I know should use generic lists, just trying to learn ArrayList also

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by stevespai
    pianorain: I know should use generic lists, just trying to learn ArrayList also
    I almost never object to someone learning something, but, in my personal opinion, ArrayList ranks right up there with gets(). There is no time when you should prefer ArrayList over a generic list; only use ArrayList if you have to run on the .Net 1.1 runtime. There are small performance differences between the two (ArrayList is generally slower because of the constant casting and boxing of value types, but generic lists are slower to create), but the type-safeness of generic lists just helps you avoid so many other errors. For instance, it's a lot easier to write code that accidently inserts an int into an ArrayList rather than into a generic list. You won't catch the ArrayList error until you get a run-time cast error from the int, while the generic list error will fail at compile-time.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but, in my personal opinion, ArrayList ranks right up there with gets().
    It makes sense to understand the workings of both, whether they're recommended or not. ArrayList is very common in older C# code, and still pretty common in newer code too.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector : accessing values
    By rahulsk1947 in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2009, 09:26 PM
  2. ArrayList Basics - Example
    By wbeasl in forum C# Programming
    Replies: 2
    Last Post: 12-26-2008, 03:41 PM
  3. Problems accessing logged on user via System.Security
    By conor20_ie in forum C# Programming
    Replies: 5
    Last Post: 11-16-2007, 06:52 AM
  4. Trouble using ArrayList
    By stevespai in forum C# Programming
    Replies: 12
    Last Post: 07-31-2006, 11:17 AM
  5. Getting an element from an ArrayList
    By osal in forum C# Programming
    Replies: 4
    Last Post: 08-03-2005, 09:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21