Rectangular Array

G

Guest

Hello, Newsgroupians:

I have a question regarding rectangular arrays. Suppose I am trying to
create a rectangular array of type System.Object. Each item in the
rectangular array may be an integer, a double, or a string. However, until
runtime, the array items are not known, so it will be a form of late binding.
What's the best way to create such a rectangular array? The way I can
picture it is as follows...

// For simplicity, assume the array to be 3x3.
object[,] arr = new object[3,3];
arr[0, 0] = 6;
arr[0, 1] = 2.5
arr[0, 2] = "Hello, World";
arr[1, 0] = "Jones";
arr[1, 1] = 36.3";
arr[1, 2] = "Hello, again";
....

Is this the proper way, or should I use an ArrayList that contains an
multitude of ArrayLists? Or is there a better approach I should be taking?
Thank you.


Trecius
 
N

Nicholas Paldino [.NET/C# MVP]

Trecius,

Well, if there is any way that you can determine the format beforehand,
perhaps by using a structure, that would be the best option. If you truly
want to use a rectangular array, then what you have is best. If you have an
ArrayList within an ArrayList, then that will be a jagged array, and you
could have different dimensions for each row (or column, depending on how
you are looking at it). You would also access it like so:

// Assuming arr is an array list of array lists.
ArrayList arr2 = arr[0];
object val = arr2[0];

It would just be a pain, as you would have to pull out the array list
every time. You might be better off with a List<List<object>>, which would
allow you to access it directly like so:

// Assuming arr is List<List<object>>
object val = arr[0][0];
 
G

Guest

Thank you, Mr. Paldino.

Nicholas Paldino said:
Trecius,

Well, if there is any way that you can determine the format beforehand,
perhaps by using a structure, that would be the best option. If you truly
want to use a rectangular array, then what you have is best. If you have an
ArrayList within an ArrayList, then that will be a jagged array, and you
could have different dimensions for each row (or column, depending on how
you are looking at it). You would also access it like so:

// Assuming arr is an array list of array lists.
ArrayList arr2 = arr[0];
object val = arr2[0];

It would just be a pain, as you would have to pull out the array list
every time. You might be better off with a List<List<object>>, which would
allow you to access it directly like so:

// Assuming arr is List<List<object>>
object val = arr[0][0];

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Trecius said:
Hello, Newsgroupians:

I have a question regarding rectangular arrays. Suppose I am trying to
create a rectangular array of type System.Object. Each item in the
rectangular array may be an integer, a double, or a string. However,
until
runtime, the array items are not known, so it will be a form of late
binding.
What's the best way to create such a rectangular array? The way I can
picture it is as follows...

// For simplicity, assume the array to be 3x3.
object[,] arr = new object[3,3];
arr[0, 0] = 6;
arr[0, 1] = 2.5
arr[0, 2] = "Hello, World";
arr[1, 0] = "Jones";
arr[1, 1] = 36.3";
arr[1, 2] = "Hello, again";
...

Is this the proper way, or should I use an ArrayList that contains an
multitude of ArrayLists? Or is there a better approach I should be
taking?
Thank you.


Trecius
 
M

Mythran

Trecius said:
Hello, Newsgroupians:

I have a question regarding rectangular arrays. Suppose I am trying to
create a rectangular array of type System.Object. Each item in the
rectangular array may be an integer, a double, or a string. However,
until
runtime, the array items are not known, so it will be a form of late
binding.
What's the best way to create such a rectangular array? The way I can
picture it is as follows...

// For simplicity, assume the array to be 3x3.
object[,] arr = new object[3,3];
arr[0, 0] = 6;
arr[0, 1] = 2.5
arr[0, 2] = "Hello, World";
arr[1, 0] = "Jones";
arr[1, 1] = 36.3";
arr[1, 2] = "Hello, again";
...

Is this the proper way, or should I use an ArrayList that contains an
multitude of ArrayLists? Or is there a better approach I should be
taking?
Thank you.


Trecius

Just a FYI:

You can declare the object array and assign the elements to the array at the
same time....IE:

object[,] arr = new object[,] {
{ 6, 2.5, "Hello, World" },
{ "Jones", 36.3, "Hello, again" }
};


HTH :)

Mythran
 

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