469,290 Members | 1,913 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

creating two-dimensional array...

Hi,

Is it possible to create two -dimensional array using ArrayList in C#? I
know you can do one-dimensional array but i dont know how to do
two-dimensional. in my case my number of columns remain same, but # of rows
are variable. Right now I'm using DataTable for this but my feeling is it is
not very efficient and may cause some problems.

if you have done something like this before could you please tell me how to
do this or if using DataTable is an efficient way?
Nov 17 '05 #1
3 30475
ZeroVisio wrote:
Hi,

Is it possible to create two -dimensional array using ArrayList in C#? I
know you can do one-dimensional array but i dont know how to do
two-dimensional. in my case my number of columns remain same, but # of rows
are variable. Right now I'm using DataTable for this but my feeling is it is
not very efficient and may cause some problems.

if you have done something like this before could you please tell me how to
do this or if using DataTable is an efficient way?

How about this one.
int dimension=4;
// Place Testing Code Here
int[][] jaggedTwo = new int[dimension][];

for(int i=0; i<dimension; i++)
{
jaggedTwo [i] = new int[] {1, 2, 3};
}
John
Nov 17 '05 #2

"ZeroVisio" wrote...
Hi,

Is it possible to create two -dimensional array using ArrayList in C#? I
know you can do one-dimensional array but i dont know how to do
two-dimensional. in my case my number of columns remain same, but # of
rows
are variable. Right now I'm using DataTable for this but my feeling is it
is
not very efficient and may cause some problems.

if you have done something like this before could you please tell me how
to
do this or if using DataTable is an efficient way?


Of course you can use ArrayLists in that way, e.g.

================================
ArrayList rows = new ArrayList();

// Iterate for each new row...

ArrayList singlerow = new ArrayList();

// Iterate for each "column"...

singlerow.Add(yourItem);

// when your row is finished...

rows.Add(singlerow);

// When you want to retrieve the data...

object o = ((ArrayList)rows[0])[0];

======================================
You can also use an ordinary array as the single row, and put that into an
ArrayList:
ArrayList rows = new ArrayList();

// Iterate for each new row...

object[] singlerow = new object[4];

// Iterate for each "column"...

singlerow[0] = yourItem;

// when your row is finished...

rows.Add(singlerow);

// When you want to retrieve the data...

object o = ((object[])rows[0])[0];

======================================

In these examples I used object as the datatype, but of course you can use
the type of the actual data here...

If you use one of those, or your DataTable solution, or another
construction, really depends on how you will use the data in the end.
// Bjorn A
Nov 17 '05 #3
Bjorn,

That was really helpful and it works in my situation.

Thanks a lot!

"Bjorn Abelli" wrote:

"ZeroVisio" wrote...
Hi,

Is it possible to create two -dimensional array using ArrayList in C#? I
know you can do one-dimensional array but i dont know how to do
two-dimensional. in my case my number of columns remain same, but # of
rows
are variable. Right now I'm using DataTable for this but my feeling is it
is
not very efficient and may cause some problems.

if you have done something like this before could you please tell me how
to
do this or if using DataTable is an efficient way?


Of course you can use ArrayLists in that way, e.g.

================================
ArrayList rows = new ArrayList();

// Iterate for each new row...

ArrayList singlerow = new ArrayList();

// Iterate for each "column"...

singlerow.Add(yourItem);

// when your row is finished...

rows.Add(singlerow);

// When you want to retrieve the data...

object o = ((ArrayList)rows[0])[0];

======================================
You can also use an ordinary array as the single row, and put that into an
ArrayList:
ArrayList rows = new ArrayList();

// Iterate for each new row...

object[] singlerow = new object[4];

// Iterate for each "column"...

singlerow[0] = yourItem;

// when your row is finished...

rows.Add(singlerow);

// When you want to retrieve the data...

object o = ((object[])rows[0])[0];

======================================

In these examples I used object as the datatype, but of course you can use
the type of the actual data here...

If you use one of those, or your DataTable solution, or another
construction, really depends on how you will use the data in the end.
// Bjorn A

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Altramagnus | last post: by
4 posts views Thread by Cal | last post: by
12 posts views Thread by Mats Lycken | last post: by
7 posts views Thread by Nathan Sokalski | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.