how to declare 2 dimensional arraylists in C#
-
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.
-
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.
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?
-
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?
thanks. i will try that out.
Keshav V. Kamat Systems Engineer Siemens India.
-
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.
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();
}- 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);