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?
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.
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.
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.
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);
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);
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.
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>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With