Reputation: 7
I have a method I have created that uses two arrays to see if a certain condition is met (this method is a test method I created to help isolate the problem, but hasn't resulted in much success). Everytime I try to run the code, I get the error
java.lang.ArrayIndexOutOfBoundsException: 3
at Player.testRNumber(Player.java:150)
at GameOfSticks1.HumanVsAI(GameOfSticks1.java:79)
at GameOfSticks1.main(GameOfSticks1.java:216)
I am pretty new to java, and after an hour of trying to figure out what is going on, have had little success. Any help would really be appreciated. Here's the method I've been using.
public void testRNumber()
{
CurrentScore[0] = 1
int x = CurrentScore[0]; //this equals 12 btw
int y = CurrentScore[1]; //this equals 3 btw
System.out.println(x);
System.out.println(y);
System.out.println((ArrayOfBuckets[x][y]) + 2);
//everything above this comment**strong text** works fine.
if(ArrayOfBuckets[x-1][y] == 1)
System.out.println("Ok, so this if loop seems fine.");
}
Upvotes: 0
Views: 48
Reputation: 4463
Here's your issue. Your 2D Array of Buckets is not rectangular.
2D Arrays in java do not have to be rectangular. My guess is that your array looks something like this:
{
...//Buckets 1 - 10
{value0, value1, value2}//bucket 11 has < 4 elements.
{value3, value4, value5, value6,...}//bucket 12 has >= 4 elements.
//Rest of buckets
}
This means calling ArrayOfBuckets[12][3]
works but calling ArrayOfBuckets[11][3]
does not.
Upvotes: 1
Reputation: 13
As written in exception message, you are trying to access element above array. It seems your ArrayOfBuckets has a size of [any][3], so the maximum second index value is 2. Please show the rest of your code (at least arrays initialization) to be sure.
Upvotes: 0