java.lang.ArrayIndexOutOfBoundsException: 90 - rotating n*m array [duplicate] - java

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 5 years ago.
I asked yesterday for a solution for rotating an 2d array with n*m.
I get this link as answer: How do you rotate a two dimensional array?
I tryed my best and I thougt it works fine. And yes it works for an n*n array but if n and m are different I get an IndexOutOfBounds Error and I have no Idea why.
Here is my code:
public void rot90DegRight(){
//get Matrix
this.Matrix = getMatrix();
int rows = Matrix.length;
int cols = Matrix[0].length;
// create a mirror of current matrix
RGBColor[][] mirror = getMatrix();
// create a new matrix
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
Matrix[j][rows - i - 1] = mirror[i][j];
}
}
// replace cols count with rows count
int tmp = rows;
rows = cols;
cols = tmp;
}
Thank you a lot for helping.

Because, when you rotate a 2d array, the rows becomes columns and the columns becomes the row. Rotating in the same matrix is possible only if n==m.
If n!=m then you need to declare a new 2d array.

Related

Create 2D array with 2 int arrays

I want to build a 2D matrix with sum of 2 input arrays(by adding row & column elements).
For example, if a is {1,3,5} and b is {4,8}, I want to build my matrix as so: [{1,3,5}, {4,7,9}, {8,11,13}].
Some more explanation here: [{1,3,5} (this is a), {4,7,9} (4 from b, 7=3+4) (9= 5+4), {8,11,13}]. (8 from b again, then 11= 3+8) (13=5+8). If you draw the first row with a, then draw b as the column aligned with a[0], you could add row element with column element to get the sum matrix.
Is there a more efficient way to do so? I tried to write 2 for-loops which gives a large time and space complexity.
public void BuildMatrix (int[] a, int[] b) {
int rows = b.length+1;
int columns = a.length;
int[][] matrix = new int[b.length+1][a.length];
for (int i = 0; i < columns-1; i++) {
matrix[0][i] = a[i];
for (int j = 1; j < rows; j++) {
matrix[j][0] = b[j-1];
matrix[j][i+1] = b[j-1] + a[i+1];
}
} }
Are input arrays to be of any possible size? If you know size ahead of time you can optimize that.
You are computing rows and columns twice:
int rows = b.length+1;
int columns = a.length;
int[][] matrix = new int[b.length+1][a.length];
so why not:
int rows = b.length+1;
int columns = a.length;
int[][] matrix = new int[rows][columns];
likewise you can factor out b[j-1] here (but compiler likely does the same anyway):
matrix[j][0] = b[j-1];
matrix[j][i+1] = b[j-1] + a[i+1];
Those are extremely minor bits though.
You should look into the broader picture and try parallelizing BuildMatrix calls if it's called a lot. You would then prepare a set of inputs for multiple BuildMatrix calls, and run them all on multiple processor cores (in parallel).

How to Copy two-dimensionals arrays without knowledge about their size [duplicate]

This question already has answers here:
How do I copy a 2 Dimensional array in Java?
(13 answers)
Closed 4 years ago.
I want to copy an array, but I don't know its size. I pasted the class and method below.
public MyClass {
private int map[][];
public void setMap(int[][] map){
//my code here
}
}
Of course, this way doesn't work. Because (as I said) I don't know the size.
int map[][] = new int[N][N];
for (int[] i : map)
for (int j : i)
i[j] = 1;
You can find the number of rows in your array and columns by using the .length() function. You can then initialize a 2D array with the same number of rows and columns by doing the following:
//array.length() is the number of rows.
//array[0].length() takes the first row and gives you the number of columns.
int map[][] = new int[array.length][array[0].length];
Now, to copy the array called array, you can do the following:
for (int i = 0; i < array.length(); i++){
for(int j = 0; j < array.length(); j++){
map[i][j] = array[i][j];
}
}
This is how you will copy your two-dimensional array, array, into your new array, map. Hope this helps!

Java matrix multiplication [duplicate]

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.

How do I create a 2D Array of images that are randomly distributed on the array?

I want to create a 2D array of images in my applet. I need a 4x4 grid with 4 images and 4 of each image spread out across the array randomly. there are a few answers on here but I don't understand how to use them.
You can declare
Image[][] myImages = new Image[4][4]
and then assign it a value as following.
myImages[0][1] = "image1"
......
or you can use a loop,
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
myImages[i][j] = "whatever to get your image"
}
}

How can I print a 2d Array? [duplicate]

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));

Categories