如何创建查找数组中最大元素的方法

时间:2018-04-07 17:40:49

标签: java multidimensional-array methods

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);
    int numberOfRows, numberOfColumns;
    double arrayElements[][] = null;                                        
    int index[] = null;

    System.out.print("Enter number of rows in array: ");
    numberOfRows = keyboard.nextInt();

    System.out.print("Enter number of columns in array: ");
    numberOfColumns = keyboard.nextInt();

    arrayElements = new double[numberOfRows][numberOfColumns];              //this command allocates memory for the array arrayElements

    for (int row = 0; row < numberOfRows; row++)
    {
        for (int column = 0; column < numberOfColumns; column++)
        {
            System.out.print("Enter the Value for Row [" + row + "], Column " + "[" + column + "]: ");
            arrayElements[row][column] = keyboard.nextDouble();
        }
    }

    System.out.printf("\n Two-Dimensional Array: %d rows x %d columns\n", numberOfRows, numberOfColumns);
         for (int row = 0; row < numberOfRows; row++)
        {
            System.out.printf("Row %3d:", row);
           for (int column = 0; column < numberOfColumns; column++) 
           {
               System.out.printf("%7.1f", arrayElements[row][column] );
           }
            System.out.println();

         index = locateLargest( arrayElements );
}
}
         public static int[] locateLargest( double[][] arrayx2 ){

         }

大家好,

我正在尝试编写一种方法来查找二维数组中的最大元素,并将具有最高值的元素的索引返回到一维数组'index'。我写了签名,但有人可以帮我弄清楚如何实际编写搜索二维数组的每个元素并找到最大数字的索引位置的方法吗?

非常感谢你!

2 个答案:

答案 0 :(得分:0)

/*Finds max value in an Array*/
public int maxValue(int array[]){
  List<Integer> list = new ArrayList<Integer>();
  for (int i = 0; i < array.length; i++) {
    list.add(array[i]);
  }
 return Collections.max(list);
}

对于二维数组使用:

int maxValue = 0;
System.out.println("\nMax values in 2D array: ");
for (int i = 0; i < twodArray.length; i++) {
    for (int j = 0; j < twodArray[i].length; j++) {
        if (twodArray[i][j] > maxValue) {
           maxValue = twodArray[i][j];
        }
    }
    System.out.println("Max value of row " + i + ": " + maxValue);
}

参考:https://stackoverflow.com/a/5877271/1848929

答案 1 :(得分:0)

首先,如果您希望方法的返回数组包含最大元素和索引,则返回类型应为 double [] 而不是 int [] 因为矩阵元素的类型是双倍的。下面的方法将返回一个包含三个元素的数组。第一个元素是行索引,第二个元素是列索引,第三个元素是元素值。如果您打算使用下面的代码,请确保将代码中的返回类型和索引的类型更改为double []。我希望这有用。

    // first we assume the largest element is the one located at row 0, and 
    //column 0, then we compare it with the other elements in the matrix  
    public static double[] locateLargest( double[][] arrayx2 ){
    double max = arrayx2[0][0];
    int row = 0, col = 0;
    double[] indexAndMaxVal = new double[3];
    for (int i = 0; i < arrayx2.length; i++) {
        for (int j = 0; j < arrayx2[i].length; j++) {
            if (arraux2[i][j] > max) {
                maxValue = arrayx2[i][j];
                row = i; 
                col = j 
            }
       }     
    }
    indexAndMaxVal[0] = row;
    indexAndMaxVal[1] = col;
    indexAndMaxVal[2] = max;
    return indexAndMaxVal;
    }