Array Index Out of Bounds?

public class TetrisGrid {

private int[][] gridData = new int[9][19];

public void initializeBlack() {

for (int x = 0; x < 10; x++) {

for (int y = 0; y < 20; y++) {

updateIndex(x, y, 0);

}

}

}

public void updateIndex(int x, int y, int data) {

gridData[x][y] = data;

}

}

Its saying that 9 is out of bounds. But I declared it all the way up until 9. o.O Probably an easy question to answer...

[479 byte] By [Kane635a] at [2007-10-3 2:48:21]
# 1

> public class TetrisGrid {

>

>private int[][] gridData = new int[9][19];

>public void initializeBlack() {

> for (int x = 0; x < 10; x++) {

>for (int y = 0; y < 20; y++) {

> updateIndex(x, y, 0);

> }

> }

>

> public void updateIndex(int x, int y, int data)

> {

>

> gridData[x][y] = data;

> Its saying that 9 is out of bounds. But I declared

> it all the way up until 9. o.O Probably an easy

> question to answer...

Yeah you declared it to have 9 slots.

0

1

2

3

4

5

6

7

8

If you want an index of 9 to be available you need to specify 10.

Norweeda at 2007-7-14 20:37:09 > top of Java-index,Java Essentials,New To Java...
# 2

hey there!

You seem to have declared an array like this:

int[][] gridData = new int[9][19];

thats pretty good. problem is, when you declare [9], java treats 0 as a valid array index. That means that your array index values start at 0 and finish at 8. The second 9 comes along, you will get array out of bounds. if you want to have 9 as a valid index, you will need to declare the array as [10]

Hope that helps.

Jason Barraclough.

fireman.sparkeya at 2007-7-14 20:37:09 > top of Java-index,Java Essentials,New To Java...
# 3
Hey SpakeyAnything in your answer that wasn't already said in reply #1?
floundera at 2007-7-14 20:37:09 > top of Java-index,Java Essentials,New To Java...