- Home /
2D Jagged Array , Array index is out of range.
Hi , i have this 2D Array which is giving me a Array index is out of range, problem. What is the cause of this problem and how can i solve this ?
void Debugging_One() {
for(int a = 0; a < numOfPlatforms; a = a + 1) {
randomXPosSegments = new int[a][];
randomXSizeSegments = randomXSize * 2;
//Debug.Log(a);
//Debug.Log(randomXSizeSegments);
for(int b = 0; b < randomXSizeSegments; b = b + 1) {
// randomXPosSegments[a][b] = 0;
randomXPosSegments[a] = new int[] {(int)(randomXSize - 0.5)};
//Debug.Log(b);
}
}
}
What line throws the exception?
note that a=a+1 is equivalent to a++
IndexOutOfRangeException: Array index is out of range.
(wrapper stelemref) object:stelemref (object,intptr,object)
Level_Generator.Debugging_One () (at Assets/_Script/Level_Generator.cs:157)
Level_Generator.Update () (at Assets/_Script/Level_Generator.cs:98)
Level_Generator.cs:157
is
randomXPosSegments[a] = new int[] {(int)(randomXSize - 0.5)};
Answer by Habitablaba · Oct 31, 2014 at 12:22 AM
Below I have annotated your code a bit.
for(int a = 0; a < numOfPlatforms; a = a + 1) {
// you are starting a at 0, so the first time through you are creating
// a 2d array where the first size is 0 and the 2nd size is nothing...
// the second time through it is 1, and the third it is 2.
randomXPosSegments = new int[a][];
randomXSizeSegments = randomXSize * 2;
for(int b = 0; b < randomXSizeSegments; b = b + 1) {
// ok, so now we're creating the 2nd part of the array
// we are giving it a size of 1, and we're overwriting it
// every iteration of the the inner loop.
// at a = 0 (the first iteration), you are accessing the 0th
// element of an array of size 0. This will cause issues.
randomXPosSegments[a] = new int[] {(int)(randomXSize - 0.5)};
}
}
Answer by dCalle · Jul 26, 2016 at 09:39 AM
I can't read anything from that. but let me guess, right now it doesn't throw an error, right?^^
alright, let me rewrite it the way I think it is right^^
for(int a = 0; a < numOfPlatforms; a++)
{
randomXSizeSegments = randomXSize* 2;
// you forgot to initilialize the second part of the 2D Array
randomXPosSegments = new int[a, randomXSizeSegments];
for(int b = 0; b < randomXSizeSegments; b++)
{
randomXPosSegments[a, b] = new int[] {(int)(randomXSize - 0.5)};
}
}
Hopefully that solves the problem. otherwise, you really need to give more information. like in which line the error is thrown.
Uhm, it does throw an error ^^. @Habitablaba kind of explained the problem about two years ago in his answer. He basically has the array-creating inside the loops and used the wrong size for the arrays. He used the current iteration index as size.
Your example doesn't use a Jagged array but a multidimensional array. Also you do the same thing wrong. You recreate and overwrite the array each "a"-iteration. You also pass the current index ("a") as size for the first dimension. Since a is "0" the first iteration your second loop would also throw an error since the index "0" isn't valid in an empty array.
If you use a multidimansional array it should be created once outside all for loops. $$anonymous$$ultidimensional arrays have a small limitation: They can only define a rectangle / cube / hypercube... Jagged arrays allows you to have different element counts in the nested arrays.
For example:
int[][] array = new int[3][];
array[0] = new int[2];
array[1] = new int[5];
array[2] = new int[3];
So element 0 of the outer array only has two sub elements while element 1 has 5. Graphically that array would look like that:
[
[0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0],
]
A multidim array would look like this:
int[,] array = new int[3,5];
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
]
As you can see each "row" always have the same element count as it forms a rectangle
Answer by Bunny83 · Jul 26, 2016 at 10:19 AM
Not sure why this got bumped after so long, but since there's still no accepted answer, here's how you should do this;
void Debugging_One()
{
// create the outer array only "once" and give it the right size:
randomXPosSegments = new int[numOfPlatforms][];
for(int a = 0; a < numOfPlatforms; a++)
{
randomXSizeSegments = randomXSize * 2;
// For each element in the outer array we have to create an inner array with the desired size:
randomXPosSegments[a] = new int[randomXSizeSegments];
for(int b = 0; b < randomXSizeSegments; b++)
{
// now do whatever you want to do with your elements
randomXPosSegments[a][b] = {(int)(randomXSize - 0.5)};;
}
}
}
From the original code it's hard to tell what you actually want to do. At the moment we create an outer array with "numOfPlatforms" inner arrays. The inner arrays will have a length of "randomXSizeSegments". I just deduced those from your for-loop limits. If "randomXSize" is a constant (at least there's no code that changes it during the iterations) all inner arrays will have the same size. However if you plan to change "randomXSize" in each "a"-iteration, each nested array will have a different size. Keep that in mind when you later work with the array.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Determining groups in a 2D array by checking neighbours (and their neighbours etc) 2 Answers
Wheel Colliders & Jumping. 0 Answers