【问题标题】:How to Create Method for Finding Largest Element in Array如何创建在数组中查找最大元素的方法
【发布时间】:2018-04-07 17:40:48
【问题描述】:
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'。我已经写好了签名,但是谁能帮我弄清楚如何实际编写搜索二维数组的每个元素并找到最大数字的索引位置的方法?

非常感谢!

【问题讨论】:

  • 感谢您的回答。现在我收到以下错误:不兼容的类型:int[] 无法转换为 double[]。也许我需要更改我的参数?

标签: java multidimensional-array methods


【解决方案1】:
/*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

【讨论】:

  • int maxValue = Integer.MIN_VALUE; 怎么样?
【解决方案2】:

首先,如果您希望方法的返回数组包含最大元素以及索引,则返回类型应为 double[] 而不是 int[] 因为矩阵元素的类型是双精度的。下面的方法将返回一个包含三个元素的数组。第一个元素是行索引,第二个是列索引,第三个是元素值。如果您打算使用下面的代码,请确保将代码中的返回类型以及 index 的类型更改为 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;
    }

【讨论】:

    猜你喜欢
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多