Java新手作业求助:矩阵删除最大值方法无法正确实现
Hey there! Let's get your DeleteMaxElem method working correctly. I can see you're a Java beginner tackling an assignment, and your current code has a few syntax and logical missteps that are blocking it from deleting the matrix's maximum values properly. Let's break down the issues first, then fix the code.
Issues in Your Current Code
- Invalid Syntax:
matrix = Integer.MAX_VALUE(matrix);is totally wrong—Integer.MAX_VALUEis a constant (the largest possible int value), not a method you can call on a matrix. You need to find the maximum value by iterating through the matrix first. - Broken Loop Variables:
for (int j = matrix; j < size; j++)tries to assign a 2D array to an int variablej, which will throw a compile error. Loop indices need to start at an integer like 0. - Misaligned Logic: Your current code checks for columns that are all zeros and deletes them, which has nothing to do with removing the maximum value from the matrix.
Corrected Method Implementation
Let's implement a method that does exactly what you need: finds the maximum value in the matrix, then creates a new matrix excluding all instances of this value. Here's the fixed code:
private static void DeleteMaxElem(int[][] matrix, int originalSize) { // Step 1: Find the maximum value in the matrix int maxValue = Integer.MIN_VALUE; for (int i = 0; i < originalSize; i++) { for (int j = 0; j < originalSize; j++) { if (matrix[i][j] > maxValue) { maxValue = matrix[i][j]; } } } // Step 2: Count how many elements are NOT the maximum value int nonMaxCount = 0; for (int i = 0; i < originalSize; i++) { for (int j = 0; j < originalSize; j++) { if (matrix[i][j] != maxValue) { nonMaxCount++; } } } // Edge case: if all elements are the maximum, we can't create a valid matrix if (nonMaxCount == 0) { System.out.println("Matrix only contains maximum values—nothing to delete."); return; } // Step 3: Calculate dimensions for the new matrix (as close to square as possible) int newRows = (int) Math.sqrt(nonMaxCount); while (nonMaxCount % newRows != 0) { newRows--; } int newCols = nonMaxCount / newRows; // Step 4: Populate the new matrix with non-maximum elements int[][] newMatrix = new int[newRows][newCols]; int currentRow = 0; int currentCol = 0; for (int i = 0; i < originalSize; i++) { for (int j = 0; j < originalSize; j++) { if (matrix[i][j] != maxValue) { newMatrix[currentRow][currentCol] = matrix[i][j]; currentCol++; // Move to next row when we reach the end of the current column if (currentCol == newCols) { currentCol = 0; currentRow++; } } } } // Step 5: Print the resulting matrix System.out.println("Matrix without maximum value (" + maxValue + "): "); for (int i = 0; i < newRows; i++) { for (int j = 0; j < newCols; j++) { System.out.print(newMatrix[i][j] + " "); } System.out.println(); } }
Alternative: Remove Entire Rows/Columns with the Maximum Value
If your assignment requires deleting whole rows and columns that contain the maximum value (instead of individual elements), use this version instead:
private static void DeleteMaxElem(int[][] matrix, int originalSize) { // Step 1: Find the maximum value int maxValue = Integer.MIN_VALUE; for (int i = 0; i < originalSize; i++) { for (int j = 0; j < originalSize; j++) { if (matrix[i][j] > maxValue) { maxValue = matrix[i][j]; } } } // Step 2: Track which rows and columns contain the maximum value boolean[] rowsToDelete = new boolean[originalSize]; boolean[] colsToDelete = new boolean[originalSize]; for (int i = 0; i < originalSize; i++) { for (int j = 0; j < originalSize; j++) { if (matrix[i][j] == maxValue) { rowsToDelete[i] = true; colsToDelete[j] = true; } } } // Step 3: Count remaining valid rows and columns int newRowCount = 0; for (boolean delete : rowsToDelete) { if (!delete) newRowCount++; } int newColCount = 0; for (boolean delete : colsToDelete) { if (!delete) newColCount++; } // Edge case: if all rows/columns are marked for deletion if (newRowCount == 0 || newColCount == 0) { System.out.println("All rows or columns contain the maximum value—resulting matrix is empty."); return; } // Step 4: Build the new matrix excluding marked rows/columns int[][] newMatrix = new int[newRowCount][newColCount]; int newRow = 0; for (int i = 0; i < originalSize; i++) { if (rowsToDelete[i]) continue; // Skip rows marked for deletion int newCol = 0; for (int j = 0; j < originalSize; j++) { if (colsToDelete[j]) continue; // Skip columns marked for deletion newMatrix[newRow][newCol] = matrix[i][j]; newCol++; } newRow++; } // Step 5: Print the result System.out.println("Matrix without rows/columns containing maximum value (" + maxValue + "): "); for (int i = 0; i < newRowCount; i++) { for (int j = 0; j < newColCount; j++) { System.out.print(newMatrix[i][j] + " "); } System.out.println(); } }
Quick Fix for Your Main Method
I also noticed a small mistake in your main method: you're calling matrix = RandomedMatrix(matrix, size); twice. This overwrites the first randomized matrix—you only need to call RandomedMatrix(matrix, size); once (assuming that method populates the matrix in-place, which is standard for such methods).
内容的提问来源于stack exchange,提问作者Timur Ivanov

