【问题标题】:C# String array inside int arraysC# 字符串数组在 int 数组中
【发布时间】:2020-07-18 10:20:18
【问题描述】:

我需要 C# 中的 integer arraysstring 中的 array

[
    ["Item 1",[1,2,3,4,5,6]],
    ["Item 1",[1,2,3,4,5,6]],
    ["Item 1",[1,2,3,4,5,6]],
    ["Item 1",[1,2,3,4,5,6]]
]

我想不出任何正确的方法来解决这个问题。我来到了下面这个sn-p。

string[,] items = new string[10, 2];
for(int 1 = 0; i<10; i++){
    items[i, 0] = "Item " + i;
    items[i, 1] = new int[10];
}

我怎样才能得到我需要的数组?

【问题讨论】:

    标签: c# arrays string int


    【解决方案1】:

    使用字典。

    Dictionary<string, int[]> myDictionary = new Dictionary<string, int[]>();
    
    // Add Item:
    myDictionary.Add("Item 1", new int[]{1,2,3,4,5,6});
    

    请注意,如果您使用字典,则键(字符串)必须是唯一的。

    或使用元组:

    Tuple<string, int[]>[] myTuples = new Tuple<string, int[]>[5];
    myTuples[0] = new Tuple<string, int[]>("Item 1", new int[] { 1, 2, 3, 4, 5, 6 });
    // Access string with: myTuples[0].Item1
    // Access int[]  with: myTuples[0].Item2
    

    或者从 C#7.0 开始,您可以使用命名元组:

    (string MyString, int[] MyIntArray)[] myTuples = new (string MyString, int[] MyIntArray)[5];
    myTuples[0] = ("Item1", new int[]{1,2,3,4,5,6});
    
    // Access string with: myTuples[0].MyString
    // Access int[]  with: myTuples[0].MyIntArray
    

    【讨论】:

    • 还可以使用 string 和 int[] 属性定义您自己的类。
    【解决方案2】:

    你可以像这样创建一个类

    public class Test
    {
        public string Name { get; set; }
        public int[] Array { get; set; }
    }
    
    List<Test> testList = new List<Test>();
    for (int i = 0; i < 4; i++)
    {
        Test test = new Test
        {
            Name = "Item 1",
            Array = new int[6]
        };
        for (int j = 0; j < 6; j++)
        {
            test.Array[j] = j + 1;
        }
        testList.Add(test);
    }
    foreach (var item in testList)
    {
        Console.Write(item.Name);
        foreach (var arr in item.Array)
        {
            Console.Write("\t" + arr);
        }
        Console.WriteLine();
    }
    

    结果

    Item 1  1       2       3       4       5       6
    Item 1  1       2       3       4       5       6
    Item 1  1       2       3       4       5       6
    Item 1  1       2       3       4       5       6
    
    

    【讨论】:

    • 为什么不使用结构体而不是类?
    【解决方案3】:

    你可以创建一个元组数组:

    (string,int[])[] items = new (string,int[])[10];
    for(int i = 0; i < items.Length; i++)
     {
     items[i] = ("Item" + i,new int[10]);
     }
    

    您可以通过 .Item1 和 .Item2 访问值:

      Console.WriteLine(items[4].Item1); // will output "Item 4"
      Console.WriteLine(items[4].Item2[4]); // will output the 4th value of the 4th array
    

    为了进一步提高可读性,您可以创建自己的类并制作此类的数组:

    class Item
     {
     public string Name {get;set;}
     public int[] Values {get;set;}
     }
    

    【讨论】:

    • 你也可以将元组的各个部分命名为var items = new (string Name, int[] Values)[10];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    • 2015-02-07
    相关资源
    最近更新 更多