Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to declare 2 dimensional arraylists in C#

how to declare 2 dimensional arraylists in C#

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Keshav V Kamat 0
    wrote on last edited by
    #1

    hello all, I am new to C# and would like your help in this matter. I know this is the syntax to declare a single dimensional arraylist in C# ArrayList arraylist_name = new ArrayList(). Can anybody tell me as to how I can declare a 2 dimensional arraylist and access the same? Thanks in advance.

    Keshav V. Kamat Systems Engineer Siemens India.

    W W 2 Replies Last reply
    0
    • K Keshav V Kamat 0

      hello all, I am new to C# and would like your help in this matter. I know this is the syntax to declare a single dimensional arraylist in C# ArrayList arraylist_name = new ArrayList(). Can anybody tell me as to how I can declare a 2 dimensional arraylist and access the same? Thanks in advance.

      Keshav V. Kamat Systems Engineer Siemens India.

      W Offline
      W Offline
      WillemM
      wrote on last edited by
      #2

      The best way to create that is to use the hashtable, although its a map and not a list you can still access the keys as if they were a list of values. Hashtable table = new Hashtable(); To create a two dimensional arraylist you will have to write something like this: ArrayList list1 = new ArrayList(); for(int x = 0; x < 10; x++) { list1.Add(new ArrayList()); } but this uses significant more memory and is not advisable if you only need the first lists index to access the second list. In that case you are better of with a hashtable that uses an Int32 value as key and ArrayList as value.

      WM. What about weapons of mass-construction?

      K 1 Reply Last reply
      0
      • W WillemM

        The best way to create that is to use the hashtable, although its a map and not a list you can still access the keys as if they were a list of values. Hashtable table = new Hashtable(); To create a two dimensional arraylist you will have to write something like this: ArrayList list1 = new ArrayList(); for(int x = 0; x < 10; x++) { list1.Add(new ArrayList()); } but this uses significant more memory and is not advisable if you only need the first lists index to access the second list. In that case you are better of with a hashtable that uses an Int32 value as key and ArrayList as value.

        WM. What about weapons of mass-construction?

        K Offline
        K Offline
        Keshav V Kamat 0
        wrote on last edited by
        #3

        thanks. i will try that out.

        Keshav V. Kamat Systems Engineer Siemens India.

        1 Reply Last reply
        0
        • K Keshav V Kamat 0

          hello all, I am new to C# and would like your help in this matter. I know this is the syntax to declare a single dimensional arraylist in C# ArrayList arraylist_name = new ArrayList(). Can anybody tell me as to how I can declare a 2 dimensional arraylist and access the same? Thanks in advance.

          Keshav V. Kamat Systems Engineer Siemens India.

          W Offline
          W Offline
          wheelerbarry
          wrote on last edited by
          #4

          Ther are 2 things you could do. 1) If you know the size of the dimention you want to create, you could make an array of ArrayList.

          ArrayList []list = new ArrayList[10]; // 10 * x

          for( int i = 0; i < 10; i++ )
          {
          list[i] = new ArrayList();
          }

          1. If you want a variable length (dont know the length) you could create an ArrayList of ArrayLists.

          ArrayList list = new ArrayList(); // The main arraylist

          ArrayList subList = new ArrayList(); // Secondary list you want in the main array.

          list.Add(subList);

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups