Hello. I have a 2D Array/Matrix and I want to write a code where I can compare each value in both directions. Let me explain myself I wrote a method that prints each value of the Array with the right direction and another one with the down direction. But my problem is how to compare each value? I have a hard time implementing a code about it and I need help.
Here are the two methods
//Driver public static void main(String[] args) { int[][] grid = new int[][] { { 7, 4, 2 }, { 0, 5, 6 }, { 3, 1, 2 } }; int arraySize = grid.length; for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid.length; j++) { // I can't solve my second issue to two the first issue with dead code } } } // goes to right direction - increment j first public static int nextRightElementMatrix(int[][] array, int arraySize) { System.out.println("Printing via right direction : "); for (int i = 0; i < arraySize; i++) { for (int j = 0; j < arraySize; j++) { return array[i][j]; } } } // goes to down direction - increment i first public static int nextDownElementMatrix(int[][] array, int arraySize) { System.out.println("Printing via down direction : "); for (int j = 0; j < arraySize; j++) { for (int i = 0; i < arraySize; i++) { return array[i][j]; } } }
The first issue is that I get a dead code on j++ if I try to return the value of the element to the method. The second issue will be how to compare each value of those two methods. (I was thinking to include both of them in another a compare?)
This is where I need help, thank you.