creating two-dimensional array...

G

Guest

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?
 
J

John Sun

ZeroVisio said:
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 = new int[] {1, 2, 3};
}
John
 
B

Bjorn Abelli

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
 
G

Guest

Bjorn,

That was really helpful and it works in my situation.

Thanks a lot!

Bjorn Abelli said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top