C# Creating an array of arrays

前端 未结 4 1917
终归单人心
终归单人心 2020-11-30 07:33

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 i         


        
4条回答
  •  隐瞒了意图╮
    2020-11-30 07:41

    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...};
    

提交回复
热议问题