This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 1 year ago.
I have been trying to fill my
int[][][] sudoku = new int[5][9][9];
array with
String[][] tmp = new String[21][21];
tmp[][] holds numbers like
005700020009600020
490060010140050030
For example this code works perfect and it gives me the number I want
System.out.println(Integer.valueOf(tmp[4][10]));
But this code
//sudoku1
b=0; c=0;
for (int i = 0; i < 6; i++) {
for (int j = 9; j < 18; j++) {
sudoku[1][b][c] = Integer.valueOf(tmp[i][j]);
c++;
}
b++;
}
throws "Index 9 out of bounds for length 9" error
You have c defined outside both loops, and added 1 each execution in the inner loop. The inner loop gets executed 6*9 times, so c can reach even the value 54, but it throws exception when it reaches 9 when trying to use sudoku[1][b][c], as the array indexes for the third element of the multiarray sudoku go from 0 to 8.
Related
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 2 years ago.
I was learning about for loops and whenever I do this loop
String[] fruits = {"Apple", "Banana", "Orange"};
for (int k = fruits.length;k > 0; k--) {
System.out.println(fruits[k]);
}
and I get this error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at helloworld.HelloWorld.main(HelloWorld.java:439)
Java Result: 1
BUILD SUCCESSFUL (total time: 10 seconds)
and I don't want to do a for each loop.
Thanks in advance
Array indexes for a Java array start at 0. So if there are 3 things in the array, their indexes are 0, 1 and 2.
You've set up your loop so that it starts at k = 3, and on subsequent iterations (if it reached them), it would have k = 2 then k = 1.
But these don't match the indexes in the array. In particular, when k = 3, there's no matching entry in the array, which is what's making your program crash.
You need to change the way your loop is set up, so that it iterates k = 2, then k = 1, then k = 0. May I suggest the following change?
for (int k = fruits.length - 1; k >= 0; k--) {
Everything else can stay the same.
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 4 years ago.
I'm writing a program for class where we hard code matrices in a driver class and put the matrix operations in a matrix class. I'm running into an issue where I get the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
I'm pretty sure that it's the second for loop but I can't think of what range to do to refer to the columns of the hard coded matrix in the driver class.
row and col are both instantiated in the Matrix class and I fixed the j++ but it still gives the same error even when I'm doing a 1x2 multiplied by a 2x3, for example
public Matrix mult(Matrix m) {
// TODO: Multiply the two matrices, store the value
// in a new matrix and return that matrix
Matrix m5 = new Matrix(new int[row][col]);
for (int i = 0; i < row; i++) {
for (int j = 0; j < m.myMatrix[0].length; j++) {
for (int k = 0; k < col; k++) {
m5.myMatrix[i][j] += myMatrix[i][k] * m.myMatrix[k][j];
}
}
}
return m5;
}
I do not see that row and col are being instantiated anywhere, so I'm guessing that you are using member variables of the Matrix class. Your resulting matrix shouldn't be the same size as the original matrix unless they are both square matricies. For example, if you multiply a 4X2 matrix by a 2X3 matrix, it should result in a 4X3 matrix. Wikipedia provides an example of this.
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 5 years ago.
what I'm trying to do is filling an array of 20 integers with random numbers, and then printing out the number of times each integer repeats itself. I'm getting an error when trying to print out the number of repetitions... Here's my code:
package nivel3;
import java.util.Random;
public class exercicio3 {
public static void main(String[] args) {
int[] numeros;
int i=0;
numeros=new int[20];
Random rand=new Random();
System.out.println("FILLING ARRAY WITH 20 RANDOM INTEGERS (FROM 0-9) \n");
do {
for (i=0; i<20; i++) {
numeros[i]=rand.nextInt(10);
System.out.printf("position"+i+of numeros[20]: "+numeros[i]+"\n\n");
}
} while (i<20);
System.out.println("--------------------------------------------------------------------- \n");
System.out.println("SHOWING THE REPEATED POSITIONS (FOR EACH POSITION OF THE ARRAY)\n");
for(int j=0; j<20; j++) {
int k=0;
do {
k++;
if (numeros[j]==numeros[k])
System.out.printf("the integer in the ["+j+"] position is equal to the integer in the ["+k+"] position \n");
}while (k<20);
}
}
}
Here's the error code:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
at nivel3.exercicio3.main(exercicio3.java:29)
I see two problems here.
The first one is that this has no sense for me.
do {
for (i=0; i<20; i++) {
numeros[i]=rand.nextInt(10);
System.out.printf("position"+i+of numeros[20]: "+numeros[i]+"\n\n");
}
} while (i<20);
You should do only the "for".
The second problem I see is that line "k++" should be under the "if". Remember arrays starts from 0 to size-1 (this means you can access from numeros[0] to numeros[19]).
So in your code, when k = 19, it enters again to the for, then you add +1 so k =20.. and numeros[20] this throws ArrayIndexOutOfBoundsException
You are doing k++ before numeros[k], which is causing ArrayIndexOutOfBoundsException on the last iteration. Move k++ to the end of your loop instead of the beginning.
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 6 years ago.
I need to print this array for a tic tac toe program I am writing for APCS. I am getting an ArrayIndexOutOfBoundsException.
String[][] ticBoard = {
{"-","-","-"},
{"-","-","-"},
{"-","-","-"}
};
for(int d = 0; d < ticBoard.length; d++){
for(int r = 0; r < ticBoard.length; d++){
System.out.print(ticBoard[d][r]);
}
}
You should change d++ to r++ like #Maroun Maroun said but also:
for(int d=0; d<ticBoard.length;d++){
for(int r = 0; r<ticBoard[d].length;r++){
System.out.print(ticBoard[d][r]);
}
System.out.println();
}
Just incase your number of rows doesn't equal your number of columns.
You're using the wrong limit in the inner loop; you should be using the length of a row, not the number of columns. (Since your two-dimensional array doesn't have the same number of rows and columns, this is especially noticeable.)
Because of this, you're going off the end of the first row of the array when you try to access the 4th element. Your code specifies a maximum value for the column index of ticBoard.length (i.e. 4), which does not correspond to the actual number of items in that row (i.e. 3).
This can be fixed by looping up to the number of elements in the row (i.e. ticBoard[d].length), not the number of rows in the array (i.e. ticBoard.length)
Furthermore, you're incrementing the wrong value in the inner loop; it should be r, not d.
for(int d = 0; d < ticBoard.length; d++) {
for(int r = 0; r < ticBoard[d].length; r++) {
System.out.print(ticBoard[d][r]);
}
System.out.println(); // So that each new row gets its own line
}
This,
for(int r = 0; r < ticBoard.length; d++){
should be something like
for(int r = 0; r < ticBoard[d].length; r++){
Or, you could use Arrays.deepToString(Object[]) to print the 2d array like
System.out.println(Arrays.deepToString(ticBoard));
This question already has answers here:
post increment operator java
(3 answers)
Closed 8 years ago.
what would be the output of
1).
int j=0;
for (int i=0; i<100; i++) j=j++;
System.out.println(j);
I thought j=j++; will be equal to
int j2 = j;
j = j+1;
so I was expecting the out put would be 99. but when I compiled on eclipse output was 0.
2). and I could not understand what is the logic behind
((int)(char)(byte) -1)
When ran it on eclipse I got output as 65535.
j=j++;
is functionally equal to
int xxx = j;
j++;
j = xxx;
So the value of j stays the same. (Because the right side is evaluated first, including the increment, then the result is assigned to j)
As for ((int)(char)(byte) -1), a char is 16bit in size and unsigned, so the bit pattern of -1 results in 65535.
This is because the ++ works as follows:
a = 0;
a = a++; // a will get the assignment of the current value of a before the increment occurs so a = 0 in here
However, in the next case here:
a = 0;
a = ++a; // a will get the assignment of the incremented value of a so a = 1 in here