0

How can we initialize a multidimensional array without pre-existing values? Only the third one is correct, but it works with pre-existing values. I would like my multidimensional array to contain 10 or 20 values, and add them later on with numbers[y][x] :

int[][] numbers = new int[10][];
//List<int[]> numbers = new List<int[]>();
//int[10][]{ new int[]{}};
//correct :  { new int[] {1, 2}, new int[] {3, 4, 5} };
numbers[0][0] = 58;

Would you know how to do this? (I don't know if [,] is the same as [][] by the way)

Thanks

Jacob
  • 77,566
  • 24
  • 149
  • 228
Paul
  • 6,108
  • 14
  • 72
  • 128

2 Answers2

1

You can try initiating values this way, it is one way to create jagged-array

int[][] test = new int[10][];
test[0] = new int[20];
test[1] = new int[30];
test[2] = new[] { 3, 4, 5 };

test[0][1] = 10;
test[1][2] = 20;
Community
  • 1
  • 1
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
1

Would you know how to do this? (I don't know if [,] is the same as [][] by the way) there are not the some as int[][] test = new int[10][]; is called a jagged array (array of arrays) and int[,] is a fixed array

just declare your array as the following

 int[,] test = new int[10,30];    
test[0,1] = 10;
test[1,2] = 20;
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47