- Home /
Jagged Array
Hi I am trying to create a Jagged array, however I keep getting errors in the compiler such as invalid rank specifier.
int[][] numbers = new int[][]
{
numbers[0] = new int[] {0,0,0,0},
numbers[1] = new int[] {0,0,0,0,0,0}
};
}
numbers[0][0] = 1;
numbers[1][0] = 2;
numbers[2][0] = 3;
numbers[3][0] = 4;
numbers[0][1] = 5;
numbers[1][1] = 6;
numbers[2][1] = 7;
numbers[3][1] = 8;
numbers[0][2] = 9;
numbers[1][2] = 10;
numbers[2][2] = 11;
numbers[3][2] = 12;
numbers[0][3] = 13;
numbers[1][3] = 14;
numbers[2][3] = 15;
numbers[3][3] = 16;
numbers[0][4] = 17;
numbers[1][4] = 18;
numbers[2][4] = 19;
numbers[3][4] = 20;
numbers[0][5] = 21;
numbers[1][5] = 22;
numbers[2][5] = 23;
numbers[3][5] = 24;
Please someone tell me what I am doing wrong...
As you are using 4 elements in the first field, shouldn't you initialise them? You did 2, why not the rest?
I do not know what you mean, by initializing numbers[0] = new int[] {0,0,0,0} numbers[1] = new int[] {0,0,0,0,0,0} this creates the full 24 set 4x6 grid needed.
numbers[0][0] = 1; should be correct as it is pointing to a single int unless I am mistaken??
Absolutely not. Look at my answer, then take a look at the jagged array page on $$anonymous$$SDN
I had a look. I'm quite new to C# so I posted out of curiosity really :)
I saw the declaration on $$anonymous$$SDN which made what OP wrote look legit. I was confused as to why he initialised
numbers[0] = new int[] {0,0,0,0},
numbers[1] = new int[] {0,0,0,0,0,0}
but used
numbers[2][0] = 3;
numbers[3][0] = 4;
A 4x6 would be
numbers[0] = new int[] {0,0,0,0,0,0},
numbers[1] = new int[] {0,0,0,0,0,0}
numbers[2] = new int[] {0,0,0,0,0,0},
numbers[3] = new int[] {0,0,0,0,0,0}
I see it now, but what really confuses me is this:
The following is a declaration of a single-dimensional array that has three elements, each of which is a single-dimensional array of integers: C#
int[][] jaggedArray = new int[3][];
Isn't that actually just a 2D array?
Yes, it is. It is a single dimension array that contains arrays, soooooooo a 2D array
So, what's the point of "Jagged Array" :D
Is it simply that it doesn't have to be a block? Less padding but isn't it less efficient than a block array, choosing memory saving over processing?
Jagged arrays are usually workarounds in languages which doesn't support multidimensional arrays. And yes, they don't require one large memory block as they are build up with several smaller arrays. They also allow to have different array sizes in each sub array.
If you don't need any of those and your array isn't that large it's way easier to use a multidimensional array.
Answer by Clet_ · Jun 24, 2014 at 12:11 AM
Edited: Updated answer. Jagged array was initially wrong. Thanks to @Bunny83
Initializer are used to Initialize an array. Your declaration is all over the place. What you want to have (I think) is a int[4][6].
Your first lines should be :
int[][] numbers = new int[][] {
new int[6],
new int[6],
new int[6],
new int[6]
}
OR
new int[][] numbers = new int[][] {
new int[6],
new int[6],
new int[6],
new int[6]
}
In summary, you need to initialize each row of a jagged array in the initalizer for it to work accordingly. Also, array values are always created with the default value of the specified type, so you don't need to fill it with zeroes, since 0 is the default value of an int.
Also, by using a simple logic, you can GREATLY reduce the number of lines to initialize your array.
int count = 1;
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 4; j++) {
numbers[j][i] = count++;
}
}
Work smarter, not harder
Thanks a lot! Learning to work smarter SLOWLY haha I am doing this to replace a 500 line logic matrix... I will get there eventually!
If a day pass without me learning anything new, I consider it a bad day. Just keep improving an read, read, read.
It is very helpful to get lost in the $$anonymous$$SDN documentations.
Today I learned that you can have different array's length in a jagged array... like
int[][] jaggedArray = new int[2][] {
new int[3],
new int[5]
}
@Clet_: Could you please fix your array initialization in your answer? Because you can't initialize a jagged array like this. You always have to create all of the inner arrays manually like you did in your comment. To initialize a 4 by 6 jagged array you have to do:
int[][] numbers = new int[4][] {
new int[6],
new int[6],
new int[6],
new int[6]
};
Only multidimensional arrays can be initialized like this:
int[,] numbers = new int[4,6];
btw: If you use an array initializer you can omit the size of the array. If you specify a size explicitly the number of values in the initializer has to match the given length.
int[][] numbers = new int[][] {
new int[6],
new int[6],
new int[6],
new int[6]
};
Answer by sj631 · Sep 24, 2021 at 05:14 AM
int[][] array = new int[rowCount][];
for (int i = 0; i < rowCount; i++)
{
array[i] = new int[columnCount];
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help using LitJson 1 Answer
Index Out of Range Exception - can't figure it out 1 Answer
Distribute terrain in zones 3 Answers
Passing an Array between classes 1 Answer