C#动态命名数组名称

时间:2016-10-18 19:21:49

标签: c# arrays

我正在尝试创建一个随机1和0的二维数组。这些数组将用于打开或关闭Arduino上的I / O线。要创建的数组(height)的数量将来自UI上的文本框以及每个数组中的项目数(width)。

这意味着我不知道我将拥有多少个数组,所以我想做的是有一个循环,我在其中命名每个数组,例如a0然后是a1然后是a2等。

我试图给它们命名,但正如你在下面看到的那样,它们无法做到正确。我需要做什么?

private int[][] build_data(int height, int width)
{
    Random randNum = new Random();
    int Min = 0, Max = 2;

    var array_name = "a";

    rch_txtbx.AppendText(height.ToString() + "\r");
    rch_txtbx.AppendText(width.ToString() + "\r");

    for (int j = 0; j <= height; j++) //create a0 to ax
    {
        array_name = "a" + j; //this creates the name I want
        int[] a = new int[width]; //need to initialise each array in turn but how?

        for (int i = 0; i <= width; i++) //create number of items in each array
        {
            a[i] = randNum.Next(Min, Max);
        }
    }

/*   This is what I am trying to create arayys of random 0 and 1

    int[] a1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    int[] a2 = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
    int[] a3 = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0 };
    int[] a4 = { 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0 };
    int[] a5 = { 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0 };
    int[] a6 = { 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0 };
    int[] a7 = { 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0 };
    int[] a8 = { 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0 };
    int[] a9 = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0 };
    int[] a10 = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0 };
    int[] a11 = { 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0 };
    int[] a12 = { 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0 };
    int[] a13 = { 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0 };
    int[] a14 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    */

    //get data ready to send back
    int[][] arr = { a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14 }; 
    return arr;
}      

3 个答案:

答案 0 :(得分:3)

不是试图动态创建变量,然后在每个外部循环迭代结束时将它们添加到数组中,而是设置该数组的值:

int[][] array = new int[height][];
for (int i = 0; i < height; i++)
{
    int[] innerArray = new int[width];
    for (int j = 0; j < width; j++)
        innerArray[j] = r.Next(min, max);

    array[i] = innerArray;
}

您还可以使用Linq替换上述for循环:

            // Creates an enumerable with N 
var array = Enumerable.Repeat(0, height) items
                      // For each item create an enumerable with M items 
                      .Select(i => Enumerable.Repeat(0, width) 
                                             // Set the value of each inner item to a 
                                             // random number
                                             .Select(j => r.Next(min,max)) 
                                             // Convert to an array
                                             .ToArray()) 
                      .ToArray();

答案 1 :(得分:2)

您可以使用多维数组:

int[,] random2dArray = new int[width, height];

for (int j = 0; j <= random2dArray.GetLength(0); j++)
{
    for (int i = 0; i <= random2dArray.GetLength(1); i++)
    {
        random2dArray[j, i] = randNum.Next(Min, Max);
    }
}

有关详细信息,请查看MSDN

答案 2 :(得分:1)

根据签名

 private int[][] build_data(int height, int width)

你实际上想要创建一个数组 int[][](又名锯齿状数组)。你可以直接做到:

 // Simplest, but not thread-safe
 private static Random s_Gen = new Random();

 // static: there's no need in "this" 
 private static int[][] build_data(int height, int width) {
   int[][] result = new int[height][];

   for (int i = 0; i < result.Length; ++i) {
     int[] line = new int[width];

     for (int j = 0; j < line.Length; ++j)
       line[j] = s_Gen.Next(2); 

     result[i] = line;         
   } 

   return result;
 }

如果您想要一个名为数组的集合,我建议使用Dictionary<string, int[]>作为返回类型:

 private static Dictionary<string, int[]> build_named_data(int height, int width) {
   Dictionary<string, int[]> result = new Dictionary<string, int[]>();

   for (int i = 1; i <= height; ++i) {
     int[] line = new int[width];

     for (int j = 0; j < line.Length; ++j)
       line[j] = s_Gen.Next(2);   

     result.Add($"a{i}", line); 
   } 

   return result;
 }

 ....

 var myData = build_named_data(15, 20); 

 int[] array = build_named_data["a3"];