简体   繁体   English

C# 创建一个 arrays 的数组

[英]C# Creating an array of arrays

I'm trying to create an array of arrays that will be using repeated data, something like below:我正在尝试创建一个 arrays 数组,它将使用重复数据,如下所示:

int[] list1 = new int[4] { 1, 2, 3, 4 };
int[] list2 = new int[4] { 5, 6, 7, 8 };
int[] list3 = new int[4] { 1, 3, 2, 1 };
int[] list4 = new int[4] { 5, 4, 3, 2 };

int[,] lists = new int[4, 4] {  list1 ,  list2 ,  list3 ,  list4  };

I can't get it to work and so I'm wondering if I'm approaching this wrong.我无法让它工作,所以我想知道我是否正在接近这个错误。

What I'm attempting to do is create some sort of method to create a long list of the values so I can process them in a specific order, repeatedly.我试图做的是创建某种方法来创建一长串值,以便我可以按特定顺序重复处理它们。 Something like,就像是,

int[,] lists = new int[90,4] { list1, list1, list3, list1, list2, (and so on)};

for (int i = 0; i < 90; ++i) {
   doStuff(lists[i]);
}

and have the arrays passed to doStuff() in order.并按顺序将 arrays 传递给doStuff() Am I going about this entirely wrong, or am I missing something for creating the array of arrays?我是不是完全错了,或者我错过了创建 arrays 数组的东西?

What you need to do is this:你需要做的是:

int[] list1 = new int[4] { 1, 2, 3, 4};
int[] list2 = new int[4] { 5, 6, 7, 8};
int[] list3 = new int[4] { 1, 3, 2, 1 };
int[] list4 = new int[4] { 5, 4, 3, 2 };

int[][] lists = new int[][] {  list1 ,  list2 ,  list3 ,  list4  };

Another alternative would be to create a List<int[]> type:另一种选择是创建一个List<int[]>类型:

List<int[]> data=new List<int[]>(){list1,list2,list3,list4};

The problem is that you are attempting to define the elements in lists to multiple lists (not multiple ints as is defined).问题是您试图将列表中的元素定义为多个列表(而不是定义的多个整数)。 You should be defining lists like this.您应该像这样定义列表。

int[,] list = new int[4,4] {
 {1,2,3,4},
 {5,6,7,8},
 {1,3,2,1},
 {5,4,3,2}};

You could also do你也可以这样做

int[] list1 = new int[4] { 1, 2, 3, 4};
int[] list2 = new int[4] { 5, 6, 7, 8};
int[] list3 = new int[4] { 1, 3, 2, 1 };
int[] list4 = new int[4] { 5, 4, 3, 2 };

int[,] lists = new int[4,4] {
 {list1[0],list1[1],list1[2],list1[3]},
 {list2[0],list2[1],list2[2],list2[3]},
 etc...};

I think you may be looking for Jagged Arrays , which are different from multi-dimensional arrays (as you are using in your example) in C#.我认为您可能正在寻找Jagged Arrays ,它与 C# 中的多维数组(如您在示例中使用的)不同。 Converting the arrays in your declarations to jagged arrays should make it work.将声明中的数组转换为锯齿状数组应该可以使其工作。 However, you'll still need to use two loops to iterate over all the items in the 2D jagged array.但是,您仍然需要使用两个循环来遍历 2D 锯齿状数组中的所有项目。

This loops vertically but might work for you.这会垂直循环,但可能对您有用。

int rtn = 0;    
foreach(int[] L in lists){
    for(int i = 0; i<L.Length;i++){
          rtn = L[i];
      //Do something with rtn
    }
}

The following will give you an array in the iterator variable of foreach下面会在foreach的迭代器变量中给你一个数组

int[][] ar =
{
    new []{ 50, 50, 1 },
    new []{ 80, 40, 2 },
    new []{ 10, 60, 3 },
    new []{ 51, 38, 4 },
    new []{ 48, 38, 5 }
};

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

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