ArrayList

  • Thread starter Thread starter C# newbie
  • Start date Start date
C

C# newbie

Hi,

How can I create a 2-dimensional arraylist ?

Any link or suggestions would be apprecaited greatly.

Thanks
 
Simply have an ArrayList of ArrayLists. For example:

ArrayList alMain = new ArrayList();

for (int i = 0; i < 20; i++)
alMain.Add(new ArrayList());

- Noah Coad -
Microsoft MVP
 
Hi Mr.Coad,

Thanks for your response. However, I'm kind of confused for the
arraylist thing!
Do you know any link or resource on this. I looked at couple books they just
written some simple examples and don't talk about multi dimensional
arraylists.
You said that :

for (int i = 0; i < 20; i++)
alMain.Add(new ArrayList());

1 - will it add 20 rows to the array with 16 columns? (since as default it
assumes to create16 cells/columns when we don't specify the argument)

2 - what about an array like [100][100] ? should I increase the loop to 99 ?

Once again thanks for your help.

Newbie
 
Hi Mr.Coad,

Thanks for your response. However, I'm kind of confused for the
arraylist thing!
Do you know any link or resource on this. I looked at couple books they just
written some simple examples and don't talk about multi dimensional
arraylists.
You said that :

for (int i = 0; i < 20; i++)
alMain.Add(new ArrayList());

1 - will it add 20 rows to the array with 16 columns? (since as default it
assumes to create16 cells/columns when we don't specify the argument)

2 - what about an array like [100][100] ? should I increase the loop to 99 ?

Once again thanks for your help.

Newbie
 
Hi Mr.Coad,

Thanks for your response. However, I'm kind of confused for the
arraylist thing!
Do you know any link or resource on this. I looked at couple books they just
written some simple examples and don't talk about multi dimensional
arraylists.
You said that :

for (int i = 0; i < 20; i++)
alMain.Add(new ArrayList());

1 - will it add 20 rows to the array with 16 columns? (since as default it
assumes to create16 cells/columns when we don't specify the argument)

2 - what about an array like [100][100] ? should I increase the loop to 99 ?

Once again thanks for your help.

Newbie
 
Now, I was looking at a page it says that the ArrayList is One-Dimensional
array!!!
homepages.wmich.edu/~jsaidoo/Chapter08/Chapter8.ppt
 
It is definitely different than a normal array. For starters, since it is
an ArrayList, the number of elements is dynamic. Yes, you can make the loop
go to 99 for 100 elements. But you'll have to use it differently than a
normal two-dimension array.

The first array, alMain, contains a dynamic number of ArrayLists, which
you'd store your elements in, which is not the same behavior as at
two-dimension array. If you really need to use a dynamically sized
ArrayList, I would really recommend only having on ArrayList and then use
another object to keep track of the 'element' number, like this:

struct DynamicElement
{
public object Object;
public int Index1;
public int Index2;
}

Then just use one ArrayList, and put these "DynamicElement"s into the list
and use the Index1 and Index2 as pseudo indexers. This is just one work
around, and may not even be efficient for your application. There isn't any
direct multi-dimensional ArrayList implementation that I am aware of.
You'll just have to be creative.

Good luck.

- Noah Coad -
Microsoft MVP


C# newbie said:
Hi Mr.Coad,

Thanks for your response. However, I'm kind of confused for the
arraylist thing!
Do you know any link or resource on this. I looked at couple books they just
written some simple examples and don't talk about multi dimensional
arraylists.
You said that :

for (int i = 0; i < 20; i++)
alMain.Add(new ArrayList());

1 - will it add 20 rows to the array with 16 columns? (since as default it
assumes to create16 cells/columns when we don't specify the argument)

2 - what about an array like [100][100] ? should I increase the loop to 99 ?

Once again thanks for your help.

Newbie


Noah Coad said:
Simply have an ArrayList of ArrayLists. For example:

ArrayList alMain = new ArrayList();

for (int i = 0; i < 20; i++)
alMain.Add(new ArrayList());

- Noah Coad -
Microsoft MVP
 
Of course, it is just a single list. You can be creative to try to make it
emulate a "multi-dimensional" array, but it is still in effect a simple
list.
 
ok, thanks I'll go with it.

Noah Coad said:
Of course, it is just a single list. You can be creative to try to make it
emulate a "multi-dimensional" array, but it is still in effect a simple
list.
 
It would probably be easiest to create an arraylist of arrays. Even better would be to create an object to hold your data (like a simple struct) and then create an arraylist of objects.
 
Back
Top