How to write condition to pretend go over array size? - java

Iam doing homework and i have problem with two dimensional array. To be specific i have to find highest sum of numbers and write left hand corner and right corner. But how to write condintion when the number is in the corner ?
There is an image https://imgur.com/cHCPDuN
I already tried to start FOR from 1 not from 0 and it works but i want more effective way
int[][] pole = new int[6][6];
Random sc1 = new Random();
int a = 0;
for (int j = 1; j < pole.length - 1; j++) {
for (int i = 1; i < pole.length - 1; i++) {
a = sc1.nextInt(9) + 1;
pole[i][j] = a;
}
if (j == 4) {
for (int j1 = 0; j1 < pole.length; j1++) {
for (int i1 = 0; i1 < pole.length; i1++) {
System.out.print(pole[i1][j1] + " ");
}
System.out.println();
}
}
}
This one works but i had to used this wrong way...

Related

for loop that cycles between 0 and 1 in a 2D Array

I am currently making a chessboard and I need to assign every other cell with 1 or 0 with a double for loop.
My code looks like this:
(Processing / Java)
int[][] board = new int [8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
board[i][j] = ?;
println(board[i][j]);
}
}
int[][] board = new int[8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
board[i][j] = (j+i) % 2;
System.out.println(board[i][j]);
}
}
Looks like you're trying to assign color to a chessBoard.
0 - Black
1 - White
what you can do is,
if((i + j) % 2 == 0)
arr[i][j] = 0;
else
arr[i][j] = 1;

Filling 2D Array in Java and getting ExceptionOutOfBounds

I searched some entries, but could not answer my question correctly myself.
I'm trying to fill a 2-dimensional array with values.
As a test I'm currently doing this by trying to fill the array with the int number 1.
I do not understand my mistake.
public static void creatBoard () {
final int L = 6;
final int H = 6;
// Modell:
int [] [] board = new int [L] [H];
for (int i = 0; i<=board.length; i++) {
for (int j = 0; j<=board.length; j++) {
board [i] [j] = 1;
}
System.out.println("");
}
Use index 0 to length-1 (as array index start with 0)
public static void creatBoard () {
final int L = 6;
final int H = 6;
// Modell:
int [] [] board = new int [L] [H];
for (int i = 0; i<board.length; i++) {
for (int j = 0; j<board[i].length; j++) {
board [i] [j] = 1;
}
System.out.println("");
}
}
just debug it and you can see, that
for (int i = 0; i<=board.length; i++) {
for (int j = 0; j<=board.length; j++) {
board [i] [j] = 1;
}
System.out.println("");
}
i, and j change values from 0 to 6, it means that it get's out of arrays bounds ( you iterate over 7 lements, instead of 6 ), just remove = sign in loop bodies
for (int i = 0; i<board.length; i++) {
for (int j = 0; j<board[i].length; j++) {
board [i] [j] = 1;
}
System.out.println("");
}
Your board array is of size 6x6 hence the board.length is 6.
When you run the loop for (int j = 0; j<=board.length; ij+) it will run from 0 up to 6 but the array indexing is from 0 to 5. So when j=6, ExceptionOutOfBounds occurs since it will be referring to index board[0][6].
Change the condition in both the loops from <=board.length to <board.length

Horizontal Transformation of Array (2D)

It's a simple matter of flipping an image horizontally and/or vertically. The premise is that given a 2D integer array that was created from importing a picture, I must create a method with a int[][] param and horizontally flip it before returning void.
The syntax is below:
public static void horizontalFlip(int[][] imgArray)
{
int temp;
for (int i = 0; i < imgArray.length; i++)
{
for (int j = 0; j < imgArray[i].length / 2; j++)
{
temp = imgArray[i][j];
imgArray[i][j] = imgArray[imgArray.length - 1 - i][j];
imgArray[imgArray.length - 1 - i][j] = temp;
}
}
}
I use imgArray as the array param and use temp as a placeholder while the loop swaps pixels, or rather, that was the intention. Currently the window does nothing after prompting the flip. Can somebody help me find the problem with the logic or syntax?
Thanks in advance, please specify any details I should provide
P.S. I can confirm the unreferenced supplied code is functional and tested.
It is happening because you are using i instead of j. But i will not stop after halfway, but it is continued and re-swap the array.
Here is a correct code :
for (int i = 0; i < imgArray.length; i++) {
for (int j = 0; j < imgArray[i].length / 2; j++) {
temp = imgArray[i][j];
imgArray[i][j] = imgArray[i][imgArray.length - 1 - j];
imgArray[i][imgArray.length - 1 -j] = temp;
}
}
Or if you want to swap columns, not rows :
for (int i = 0; i < imgArray.length / 2; i++) {
for (int j = 0; j < imgArray[i].length; j++) {
temp = imgArray[i][j];
imgArray[i][j] = imgArray[imgArray.length - 1 - i][j];
imgArray[imgArray.length - 1 -i][j] = temp;
}
}
This will correctly flip the image horizontally:
public static void horizontalFlip(int[][] imgArray)
{
int temp;
for (int i = 0; i < imgArray.length; i++) {
for (int j = 0; j < imgArray[i].length/2; j++) {
temp = imgArray[i][j];
imgArray[i][j] = imgArray[i][imgArray[i].length - 1 - j];
imgArray[i][imgArray[i].length - 1 - j] = temp;
}
}
}
Please see my solution below,
for(int i=0; i<matrix.length / 2; i++)
{
int[] row = matrix[i];
int[] temp = row;
matrix[i] = matrix[matrix.length - 1];
matrix[matrix.length - 1] = row;
}

Sort an array alphabetically

I need to sort book objects by their titles in a simple way. However, the selection sort algorithm I wrote isn't working properly and just moves the books around, but with no apparent order. What am I doing wrong?
int j;
int b;
for (int i = 0; i < 20 - 1; i++) {
int minIndex = i;
for (j = i + 1; j < 20; j++) {
b = (bookA[j].getTitle().compareTo(bookA[minIndex].getTitle()));
if (b < 0) {
minIndex=j;
}
}
Book temp = bookA[i];
bookA[i] = bookA[j];
bookA[j] = temp;
}
for (int z = 0; z < 20; z++)
System.out.println(bookA[z].toString());
You're using j as an index in bookA[i] = bookA[j];. The problem is that you're overriding the value of j at every iteration, so when it finally gets to bookA[i] = bookA[j]; it will always be 20.
What you want is to replace it with bookA[minIndex]. The resulting code would look like this:
int j;
int b;
for(int i=0;i<20-1;i++){
int minIndex=i;
for(j=i+1;j<20; j++) {
b=(bookA[j].getTitle().compareTo(bookA[minIndex].getTitle()));
if(b<0){
minIndex=j;
}
}
Book temp = bookA[i];
bookA[i] = bookA[minIndex];
bookA[minIndex] = temp;
}
for(int z=0;z<20;z++)
System.out.println(bookA[z].toString());

Add Matrices in a Specific Layout

My professor wants me to print out the matrices side by side with the "+" between the two matrices and then a "=" sign. In the end he wants us to add the matrices together.
This is the work so far.
So the result would come out as:
1 2 3 9 8 7 10 10 10
4 5 6 + 6 5 4 = 10 10 10
7 8 9 3 2 1 10 10 10
enter code here public static void main(String[] args) {
int matrix1[][] = {{1,2,3},{4,5,6},{6,7,8}};
int matrix2[][] = {{9,8,7},{6,5,4},{3,2,1}};
int result1;
int[][] result2 = new int[2][3];
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[0].length; j++) {
System.out.printf(matrix1[i][j] + " ");
System.out.print("");
}
System.out.println("");
}
for (int i = 0; i < matrix2.length; i++) {
for (int j = 0; j < matrix2[0].length; j++) {
System.out.printf(matrix2[i][j] + " ");
}
System.out.println("");
}
}
My problem is, how could I print it side by side with the solutions?
Consider the two printing loops for your matrices:
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[0].length; j++) {
System.out.printf(matrix1[i][j] + " ");
}
System.out.println("");
}
for (int i = 0; i < matrix2.length; i++) {
for (int j = 0; j < matrix2[0].length; j++) {
System.out.printf(matrix2[i][j] + " ");
}
System.out.println("");
}
They print matrix 1, then 2 - and so the matrices will be below each other.
If you want the matrices side by side, you need to print line 1 of every matrix, then - after a new line - line 2 of every matrix, etc. By re-arranging how the loops go through the matrices, you could have your new layout.
You unfortunately cannot print them one at a time, you need to take it row by row. This solution requires both matrix1 and matrix2 to be of equal height. But here's a template that should get you started.
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[i].length; j++) {
}
if (i == matrix1/2) {
} else { //One part of if handles when "+" is needed, other one doesn't
}
for (int j = 0; j < matrix2[i].length; j++) {
}
if (i == matrix1/2) {
}
for (int j = 0; j < ???; j++) {
}
}

Categories