获取二维数组中某一列的最小值
Posted
技术标签:
【中文标题】获取二维数组中某一列的最小值【英文标题】:Getting the minimum value of a column in a two-dimensional array 【发布时间】:2012-02-22 19:35:18 【问题描述】:我正在尝试在二维数组 (Java) 中找到最小值。
我正在尝试使用 for 循环。我的目标是创建一个程序来获取矩阵的鞍点,但我将一步一步地进行。我已经弄清楚如何在二维数组的一行中找到最大值。然后我想在与最大值相同的列中找到最小值。
这是我的代码:
class Matrix_1
int[][] A;
int a;
int b;
int max;
int min;
int i;
int d;
Matrix_1()
super();
public int[][] createMatrix(int a, int b)
Scanner inputm = new Scanner(System.in);
A = new int[a][b];
System.out.println("Enter elements for matrix A : ");
for (int i = 0; i < A.length; i++)
System.out.println("Enter numbers for " + i + "th row");
for (int j = 0; j < A[i].length; j++)
A[i][j] = inputm.nextInt();
return A;
public int[][] displayMatrix()
System.out.println("Matrix A: ");
for (int i = 0; i < A.length; i++)
System.out.println();
for (int j = 0; j < A[i].length; j++)
System.out.print(A[i][j] + " ");
return A;
public int getMax(int c)
max = 0;
a = c;
for (i = 0; i < A[a].length; i++)
while (A[a][i] > max)
max = A[a][i];
i = d;
System.out.println(i);
System.out.println("max = " + max);
return max;
public int getMin()
max = min;
a = 0;
for (int j = 0; j < A[a].length; j++)
while (A[i][j] < min)
min = A[j][d];
System.out.println("min in row " + a + "from max number" + max + " = "
+ min);
return min;
【问题讨论】:
前两种方法工作正常。 @deporter 是的,但至少我自己尝试过。我只是想要一些帮助。 尝试将 getMin() 中的 max = min 更改为 min = max。 【参考方案1】:这将获取与最大值在同一行的最小值
public int minOnMaxRow()
int max = Integer.Min_Value();
int min;
for (int i = 0; i < A.length; i++)
for (int j = 0; j < A[i].length; j++)
if (A[i][j] > max)
max = A[i][j];
min = Integer.Max_Value();
for(k= 0; k < A[i].length; k++)
if (A[i][k] < min)
min = A[i][k]
return min;
【讨论】:
Integer.Min_Value() 是做什么的? 它是Integer类中的一个静态方法,返回一个整数可以采用的最小值,Max_Value则相反【参考方案2】:使用这些方法列出每行和每列的最大值/最小值:
public void getMaximumOfEveryColumn ()
for ( int i = 0; i < A.length; i++ )
max = Integer.MIN_VALUE;
for ( int j = 0; j < A [ i ].length; j++ )
if ( A [ j ] [ i ] > max )
max = A [ j ] [ i ];
System.out.println( "Maximum of column " + i + " = " + max );
public void getMinimumOfEveryColumn ()
for ( int i = 0; i < A.length; i++ )
min = Integer.MAX_VALUE;
for ( int j = 0; j < A [ i ].length; j++ )
if ( A [ j ] [ i ] < min )
min = A [ j ] [ i ];
System.out.println( "Minimum of column " + i + " = " + min );
public void getMaximumOfEveryRow ()
for ( int i = 0; i < A.length; i++ )
max = Integer.MIN_VALUE;
for ( int j = 0; j < A [ i ].length; j++ )
if ( A [ i ] [ j ] > max )
max = A [ i ] [ j ];
System.out.println( "Maximum of row " + i + " = " + max );
public void getMinimumOfEveryRow ()
for ( int i = 0; i < A.length; i++ )
min = Integer.MAX_VALUE;
for ( int j = 0; j < A [ i ].length; j++ )
if ( A [ i ] [ j ] < min )
min = A [ i ] [ j ];
System.out.println( "Minimum of row " + i + " = " + min );
【讨论】:
嘿,我得到了一些代码,但你介意解释一下吗。 上述解决方案不适用于非方阵。你能看看我的答案并纠正它吗?【参考方案3】:矩阵中列的最小值和最大值:-
import java.util.Scanner;
public class Max_Min_Row_Col
static int col;
static int row;
static int[][] trans_arr = new int[col][row];
public static void main(String[] args)
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
row = m;
int n = sc.nextInt();
col = n;
int[][] arr = new int[row][col];
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
arr[i][j] = sc.nextInt();
System.out.print(arr[i][j] + " ");
System.out.println();
int i_old = 0;
int m_old = arr[i_old].length;
for (int i = 0; i < m_old; i++)
int min = Integer.MAX_VALUE;
for (int j = 0; j < arr.length; j++)
if (arr[j][i] < min)
min = arr[j][i];
System.out.println("Minimum of column " + i + " = " + min);
for (int i = 0; i < m_old; i++)
int max = Integer.MIN_VALUE;
for (int j = 0; j < arr.length; j++)
if (arr[j][i] > max)
max = arr[j][i];
System.out.println("Maximum of column " + i + " = " + max);
【讨论】:
以上是关于获取二维数组中某一列的最小值的主要内容,如果未能解决你的问题,请参考以下文章
计算机二级-C语言-程序设计题-190119记录-求出一个二维数组每一列的最小值。