I have to compare elements in a multi dimensional array to check if a number is repeating either horizontally or vertically. I have written a program to compare horizontally. But how to update the current value once I have iterated over the array. Please let me know where I am going wrong.
public class TwoDimensionArray {
private static int rows = 4;
private static int columns = 4;
private static int array[][] = new int[][]{{4,3,9,1},{6,2,4,2},{5,4,5,2},{7,7,2,4}};
public static void main(String args[]){
printArray();
System.out.println("--------------------");
int counter=0;
int i=0;
int j = 0;
int current=array[i][j];
for ( i = 0; i<rows; i++){
for(j=0;j<columns; j++){
if(array[i][j]==current){
System.out.print("i > "+i+" j > "+j);
}
}
System.out.println();
}
}
private static void printArray(){
for (int i = 0; i < rows; i++){
for (int j = 0 ; j < columns; j++){
System.out.print(array[i][j]+ " ");
}
System.out.println();
}
}
}
But how to update the current value once I have iterated over the array.E.g 4 is repeated, then with what value you want to replace 4?