【发布时间】: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