简体   繁体   English

在C#中具有两种不同数据类型的二维数组

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

Can you please tell me if it is possible to have a 2D array with two day types. 你能否告诉我是否有可能有两天类型的2D阵列。 I am new to C#. 我是C#的新手。

For example: array[double][string] 例如: 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. 我想将它们放入2D数组中,并根据第一个double索引对其进行排序。 Please let me know if this is possible with an example. 如果可以举例,请告诉我。

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. 由于每个名称的项目数不同,因此2D阵列不是建模它的理想方式。 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 . 你也可以使用另一种实数表示,例如floatdouble

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. 你可以使用一个KeyValuePair<int, string>数组,但我没有太多理由去那条路,除非你只是寻找快速和肮脏的东西。

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"}
        };

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. 创建一个具有您需要存储和访问的属性的类。

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 https://msdn.microsoft.com/en-us/library/ms229017(v=vs.110).aspx

Why is 16 byte the recommended size for struct in C#? 为什么16字节是C#中struct的推荐大小?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM