Two-Dimensional Array with two different data types in C#

白昼怎懂夜的黑 提交于 2021-02-05 05:59:25

问题


Can you please tell me if it is possible to have a 2D array with two day types. I am new to C#.

For example: array[double][string]

I have radius of flowers alone with their name as follows:

4.7,Iris-setosa
4.6,Iris-setosa
7,Iris-versicolor
6.4,Iris-versicolor
6.9,Iris-versicolor
5.5,Iris-versicolor
6.5,Iris-versicolor
6.3,Iris-virginica
5.8,Iris-virginica

I would like to put these in to a 2D array and sort it according to the first double index. Please let me know if this is possible with an example.


回答1:


As comments have said, you likely want a simple class for this:

 public class Flower {
    public double Radius { get; set; }
    public string Name { get; set; }
 }

 var l = new List<Flower>();
 l.Add(new Flower() { Radius = 4.7, Name = "Iris-setosa" });
 l.Add(new Flower() { Radius = 4.6, Name = "Iris-setosa" });
 /* ... */

 Flower[] sorted = l.OrderBy(f => f.Radius).ToArray();

You could get away with an array of KeyValuePair<int, string>, but I don't see much reason to go that route unless you're just looking for something quick and dirty.




回答2:


The data that you are trying to organize looks like a list of name-value pairs, with non-unique names. Since the number of items is different for each name, 2D array is not the ideal way to model it. You would be better off with a dictionary that maps names to lists of radii as follows:

Dictionary<string,List<decimal>>

Here is how your data would be organized in such a dictionary:

var data = new Dictionary<string,List<decimal>> {
    {"Iris-setosa", new List<decimal> {4.7M, 4.6M}}
,   {"Iris-versicolor", new List<decimal> {7M, 6.4M, 6.9M, 5.5M, 6.5M}}
,   {"Iris-virginica", new List<decimal> {6.3M, 5.8M}}
};

I am assuming that the representation of the radius needs to be decimal in your case; you could use another representation of real numbers, too, such as float or double.




回答3:


If you don't want to create a class for some reason you can also use Anonymous Types, so for your case it will be something like:

        var a1 = new[] {
            new {Radius = 4.7, Name = "Iris-setosa"},
            new {Radius = 4.6, Name = "Iris-setosa"}
        };



回答4:


If this is just for temporary processing, you can also use a Tuple.

List<Tuple<decimal, string>> test = new List<Tuple<decimal, string>>();

test.Add(Tuple.Create<decimal, string>(4.7M, "Iris-setosa"));
test.Add(Tuple.Create<decimal, string>(4.6M, "Iris-setosa"));

var sortedList = test.OrderBy(i => i.Item1);

Don't pass Tuples around in your application though. Create a class that has the properties on it that you need to store and access.




回答5:


Depending on how your application will use this data, a struct (instead of a class) may also be a possible solution. The struct is passed by value and is allocated on the stack, instead of the heap. This means it is not really garbage collected but deallocated when the stack unwinds or when their containing type gets deallocated.

So, if you have a very small data structure that is not passed around much a struct may be a more memory efficient choice for you. I think the easiest way to tell is to benchmark the two alternatives and see which one works best for you.

See for further reference:

https://msdn.microsoft.com/en-us/library/ms229017(v=vs.110).aspx

Why is 16 byte the recommended size for struct in C#?



来源:https://stackoverflow.com/questions/26182824/two-dimensional-array-with-two-different-data-types-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!