57

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]);
}

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?

Aman Agnihotri
  • 2,973
  • 1
  • 18
  • 22
Fidsah
  • 819
  • 1
  • 7
  • 13

5 Answers5

79

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[]> data=new List<int[]>(){list1,list2,list3,list4};
Sean
  • 60,939
  • 11
  • 97
  • 136
  • 3
    How do I create an empty array of arrays with a fixed size? The individual arrays stored within the array may vary, but there will be a fixed amount of arrays within the master array. – Aaron Franke Nov 03 '17 at 21:19
  • 1
    @AaronFranke I think you are asking for `int[][] lists = new int[n][];` where `n` is the number of arrays in your array of arrays. – Jeanot Zubler Nov 23 '22 at 12:56
10

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...};
Suroot
  • 4,315
  • 1
  • 22
  • 28
8

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#. 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.

Noldorin
  • 144,213
  • 56
  • 264
  • 302
0

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
    }
}
Robert Quinn
  • 579
  • 5
  • 10
0

The following will give you an array in the iterator variable of foreach

int[][] ar =
{
    new []{ 50, 50, 1 },
    new []{ 80, 40, 2 },
    new []{ 10, 60, 3 },
    new []{ 51, 38, 4 },
    new []{ 48, 38, 5 }
};
Richard Collette
  • 5,462
  • 4
  • 53
  • 79