Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a multidimensional array list in c#?

Tags:

c#

arraylist

Is it possible to create a multidimensional arraylist in C#?

StartDate   Qty   size
9/1/2010    10     15
9/1/2009    12     17
9/1/2008    11     19

StartDate, Qty and size are the 3 arraylists. I need to have them in a single arraylist. I would also need to sort this arraylist by StartDate. Is this possible? Is there a better way to do it other than arraylist?

like image 720
Prady Avatar asked Sep 28 '10 18:09

Prady


People also ask

What is multidimensional array in C with example?

A multi-dimensional array is an array with more than one level or dimension. For example, a 2D array, or two-dimensional array, is an array of arrays, meaning it is a matrix of rows and columns (think of a table). A 3D array adds another dimension, turning it into an array of arrays of arrays.

How do you write a multidimensional array?

Creating Multidimensional Arrays You can create a multidimensional array by creating a 2-D matrix first, and then extending it. For example, first define a 3-by-3 matrix as the first page in a 3-D array. Now add a second page. To do this, assign another 3-by-3 matrix to the index value 2 in the third dimension.

What is the syntax of multidimensional array in C?

A multidimensional array is declared using the following syntax: type array_name[d1][d2][d3][d4]……… [dn]; Where each d is a dimension, and dn is the size of final dimension.


4 Answers

You can do it that way, yes. But in this case since each row seems related, why not create a class to hold the data:

public class Info
{
    public DateTime StartDate { get; set; }
    public int Qty { get; set; }
    public int Size { get; set; }
}

And then just have a regular List to hold the objects:

List<Info> infoList = new List<Info>();

That will save you from having to worry about the ordering of each List. To handle the ordering, you can use the LINQ to Objects Extension methods:

var sortedList = infoList.OrderBy(i => i.StartDate);
like image 116
Justin Niessner Avatar answered Oct 19 '22 08:10

Justin Niessner


When you can, go with Justin's answer. It will save headaches in the long run because each property has meaning. If you need a quick approach and you have .NET 4, you could list the Tuple in a list. Such as

List<Tuple<DateTime, int, int>> myData = new List<Tuple<DateTime, int, int>>();
myData.Add(new Tuple<DateTime, int, int>(DateTime.Now, 1, 2));

//

DateTime myDate = myData[0].Item1;
int myQty = myData[0].Item2;
int mySize = myData[0].Item3;

If you do not have .NET 4, it is trivial to implement your own tuple. However, if you are going to do that, might as well skip back to the top and go with Justin's answer.

Edit For completeness, here are sorting options using this approach.

// descending sort done in place using List<>.Sort
myData.Sort((t1, t2) => -1 * t1.Item1.CompareTo(t2.Item1)); 

// descending sort performed as necessary in a sequence
var orderedList = myData.OrderByDescending(t => t.Item1);
like image 36
Anthony Pegram Avatar answered Oct 19 '22 08:10

Anthony Pegram


You want a list of lists. No reason to use ArrayList either. From your example:

List<List<DateTime>> list = new List<List<DateTime>>();

That said, I prefer something like Justin has shown above.

like image 1
Ed S. Avatar answered Oct 19 '22 09:10

Ed S.


If these properties describe an entity or "data row", you might consider creating a class:

public class MyClass
{
    public DateTime StartDate {get;set;}
    public int Qty {get;set;}
    public int Size {get;set;}
}

You can then create an array of these objects:

List<MyClass> myClassArray = new List<MyClass>();
like image 1
Dave Swersky Avatar answered Oct 19 '22 09:10

Dave Swersky