问题描述
我正在尝试创建一个将使用重复数据的数组,如下所示:
I'm trying to create an array of arrays that will be using repeated data, something like below:
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]);
}
并将数组按顺序传递给 doStuff()
.我这样做是完全错误的,还是我在创建数组数组时遗漏了什么?
and have the arrays passed to doStuff()
in order. Am I going about this entirely wrong, or am I missing something for creating the array of 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[][] { list1 , list2 , list3 , list4 };
另一种选择是创建一个 List
类型:
Another alternative would be to create a List<int[]>
type:
List<int[]> data=new List<int[]>(){list1,list2,list3,list4};
这篇关于C# 创建一个数组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!