1

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);
    }
}
Dan
  • 3,647
  • 5
  • 20
  • 26
Vlad
  • 13
  • 3
  • You can't really remove a row from a 2d-array in java, you'll have to identify which row you'd like to remove, and then re-create the array without this row. See reference here: https://stackoverflow.com/questions/1805147/how-to-remove-a-row-from-a-2d-array#:~:text=You%20can't%20remove%20elements,one%20you%20want%20to%20remove. – idanz Apr 30 '22 at 08:48

1 Answers1

0

As @idanz has pointed out in the comments, you cannot redefine an array size, or a matrix since it's simply an array of arrays.

What you can do though is to instantiate a new array (or in this case 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", then use this index to copy every element from the original matrix into the new one except for the elements within the row to "delete", i.e. not to copy.

On a side note, I've also noticed you've improperly iterated the elements of your matrix. Your innermost loop should check the length of matrix[i].length, not matrix.length. besides, I've also tweaked some variable and method names since they didn't respect the naming conventions. Your variables and methods should start in lower case and be written in 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;
    }
}
Dan
  • 3,647
  • 5
  • 20
  • 26