0
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 ){

         }

Hello all,

I am trying to write a method for finding the largest element in a two-dimensional array, and return the index of the element with the highest value to the single-dimensional array 'index'. I have the signature written, but can anyone please help me figure out how to actually write the method that will search each element of the two-dimensional array and find the index location of the largest number?

Thank you so much!

  • Thank you for your answer. Now I am getting the following error: incompatible types: int[] cannot be converted to double[]. Do I need to change my parameters, maybe? – Merilyn Apr 07 '18 at 18:04

2 Answers2

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);
}

For 2 dimensional array use that:

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);
}

Reference: https://stackoverflow.com/a/5877271/1848929

hakki
  • 6,181
  • 6
  • 62
  • 106
0

Firstly, if you want the returned array of your method to contain the largest element as well as the index, the return type should be double[] and not int[] since the type of the elements of the matrix is double. The method bellow will return an array containing three elements. the first element is the row index, the second is the column index, and the third is the element value. if you are going to use the code bellow, make sure to change the return type and also the type of index in your code to double[]. I hope this is helpful.

    // 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;
    }
Kimiya Zargari
  • 324
  • 1
  • 14