找到矩阵的最大元素并删除具有该元素的行
Find the maximum element of the matrix and delete a row with this 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);
}
}
正如@idanz 在评论中指出的那样,您不能重新定义数组大小或矩阵,因为它只是一个数组数组。
你可以做的是实例化一个比原始矩阵少一行的新数组(或在本例中为新矩阵)。在您的搜索方法中,您可以 return 要“删除”的行的索引,然后使用此索引将原始矩阵中的每个元素复制到新矩阵中,但要“删除”的行中的元素除外,即请勿复制。
附带说明一下,我还注意到您不正确地迭代了矩阵的元素。您最内层的循环应该检查 matrix[i].length
的长度,而不是 matrix.length
。此外,我还调整了一些变量和方法名称,因为它们不符合 naming conventions。您的变量和方法应以小写字母开头,并以 camel-case 表示法编写。
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;
}
}
我需要帮助来完成删除具有最大元素的行。我设法读取用户的输入以设置矩阵维度,用随机数填充矩阵并搜索最大元素。但是,我无法删除具有最大元素的行。
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);
}
}
正如@idanz 在评论中指出的那样,您不能重新定义数组大小或矩阵,因为它只是一个数组数组。
你可以做的是实例化一个比原始矩阵少一行的新数组(或在本例中为新矩阵)。在您的搜索方法中,您可以 return 要“删除”的行的索引,然后使用此索引将原始矩阵中的每个元素复制到新矩阵中,但要“删除”的行中的元素除外,即请勿复制。
附带说明一下,我还注意到您不正确地迭代了矩阵的元素。您最内层的循环应该检查 matrix[i].length
的长度,而不是 matrix.length
。此外,我还调整了一些变量和方法名称,因为它们不符合 naming conventions。您的变量和方法应以小写字母开头,并以 camel-case 表示法编写。
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;
}
}