javaarraysmatrix

Find the maximum element of the matrix and delete a row with this element


I need help to complete the removal of the row with max element. I managed to read the user's input to set up the matrix dimension, filled the matrix with random numbers and search for the maximum element. However, I can't delete the row with the max element.

public class Main {
    static int MAX = 0;

    public static void main(String[] args) {
        System.out.println("My name");
        System.out.print("Enter N: ");

        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        Integer matrix[][] = new Integer[N][N];
        Initialization(matrix);
        Search(matrix);
    }


    static void Initialization(Integer[][] matrix) {
        Random r = new Random();
        System.out.println("Matrix before processing");
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix.length; j++) {
                matrix[i][j] = r.nextInt(100);
                System.out.print(matrix[i][j] + "\t");
            }
            System.out.print("\n");
        }
    }
    static void Search(Integer matrix[][]) {
        int max = 0;

        for(int i = 0; i < matrix.length; i++) {
            for(int j = 0; j < matrix.length; j++) {
                if (matrix[i][j] > max) {
                    max = matrix[i][j];
                }
            }
        }
        MAX = max;
        System.out.println("Max element in matrix: " + max);
    }
}

Solution

  • As @idanz has pointed out in the comments, you cannot redefine an array's size, or in your case a matrix's size (a matrix is an array of arrays).

    What you can do though is to instantiate a new matrix with one less row than the original matrix. In your search method, you could return the index of the row to "delete", and then copy the elements from the original matrix except for the ones in the returned row.

    As a side note, Your innermost loop should check the length of matrix[i].length, not matrix.length. In the code below, I've also tweaked some of the variable and method names, since they didn't abide by the naming conventions (camel-case notation).

    public class Main {
    
        public static void main(String[] args) {
            System.out.print("Enter N: ");
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
    
            //Initializing and printing the matrix
            Integer matrix[][] = new Integer[n][n];
            initialization(matrix);
            print(matrix);
    
            //Retrieving the index row with the maximum element
            int rowWithMax = search(matrix);
    
            //Retrieving a new a matrix without the row to delete and assigning it to the old matrix
            matrix = deleteRow(matrix, rowWithMax);
    
            System.out.println("\nNew matrix without the deleted row");
            print(matrix);
        }
    
    
        static void print(Integer[][] matrix) {
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix[i].length; j++) {
                    System.out.print(matrix[i][j] + "\t");
                }
                System.out.println();
            }
        }
    
        static void initialization(Integer[][] matrix) {
            Random r = new Random();
            System.out.println("Matrix before processing");
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix[i].length; j++) {
                    matrix[i][j] = r.nextInt(100);
                }
            }
        }
    
        static int search(Integer matrix[][]) {
            int max = 0, rowWithMax = -1;
    
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix[i].length; j++) {
                    if (matrix[i][j] > max) {
                        max = matrix[i][j];
                        rowWithMax = i;
                    }
                }
            }
            System.out.println("\nMax element in matrix: " + max);
            return rowWithMax;
        }
    
        static Integer[][] deleteRow(Integer matrix[][], int rowToDelete) {
            //Since in Java you cannot redefine the size of an array (in this case an array of array of int)
            //you need to instantiate a new matrix filled with the elements of the original matrix except
            //for the row with the highest element.
    
            //If no row must be deleted (i.e. no proper index has been passed) then the original matrix is simply returned
            if (rowToDelete < 0) {
                return matrix;
            }
    
            Integer[][] matrix2 = new Integer[matrix.length - 1][matrix.length];
            int displacedIndex;
            for (int i = 0; i < matrix.length; i++) {
                //Skipping the row to delete (i.e. the row which must not be copied within the new matrix)
                if (i != rowToDelete) {
                    //Since i is the row index of the original matrix, this can't be the same index for the new matrix after skipping the row to delete,
                    //so, we need to create a displaced index for the new matrix which corresponds to i before meeting the deleted row while to i-1 after
                    //meeting the deleted row
                    displacedIndex = i < rowToDelete ? i : i - 1;
    
                    for (int j = 0; j < matrix[i].length; j++) {
                        matrix2[displacedIndex][j] = matrix[i][j];
                    }
                }
            }
    
            return matrix2;
        }
    }