C#中具有两种不同数据类型的二维数组 [英] Two-Dimensional Array with two different data types in C#

查看:212
本文介绍了C#中具有两种不同数据类型的二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否请您告诉我是否可以使用两天类型的2D阵列.我是C#的新手.

例如: array [double] [string]

我一个人有半径的花朵,它们的名字如下:

  4.7,鸢尾花4.6,鸢尾7,鸢尾花6.4,鸢尾花6.9,鸢尾花5.5,鸢尾花6.5,鸢尾花6.3,鸢尾5.8,鸢尾 

我想将它们放入2D数组中,并根据第一个 double 索引对其进行排序.请通过示例告诉我是否可行.

解决方案

正如评论所说,您可能想要一个简单的类:

 公共类Flower {公共双倍半径{get;放;}公共字符串名称{get;放;}}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 []排序= l.OrderBy(f => f.Radius).ToArray(); 

您可以避免使用 KeyValuePair< int,string> 的数组,但是除非您只是在寻找快速又肮脏的东西,否则我没有太多理由这样做./p>

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.

解决方案

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.

这篇关于C#中具有两种不同数据类型的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆