Search code examples
javaarraysmultidimensional-arrayrandom

How to get the top 5 numbers from a 2D array of random numbers


I am pretty new to java and am just learning 2D arrays. I am trying to get the top 5 numbers to display from a random list. I think this could work but am not sure why I am getting an error. One other thing is that I cannot use the sort function.

Code here:

public static void main(String[] args) {
 //Random Number stuff
        Random rand = new Random();
        
        int[] large = new int [5];
        
        int max = 0, index;

        int[][] arrSize = new int [4][5];
        
        
        for (int i = 0; i < arrSize.length; i++) {
            for (int j=0; j< arrSize[i].length; j++) {
                arrSize[i][j] = rand.nextInt(89) + 10;
                
                System.out.print(arrSize[i][j] + " ");
            }
                
            System.out.println();
            
        }
        // Top 5
        for (int p = 0; p < 5; p++) {
        max = arrSize [0][0];
        index = 0;
        
        for (int i = 0; i < arrSize.length; i++) {
            for (int j = 0; j < arrSize[i].length; j++) {
                
                if (max < arrSize[i][j]) {
                    
                    max = arrSize[i][j];
                    index = i;
                    
                }
        }
        }
        
        
        large[p] = max;
        arrSize[index] = Integer.MIN_VALUE;  //Error here
        
            System.out.println("Highest Number: " + large[p]);
    }
 
}
}

Error text:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Type mismatch: cannot convert from int to int[]

    at secondAssignment.BiggestNumbersRectangular.main(BiggestNumbersRectangular.java:47)

I am not sure why I am getting an error, any help would appreciated. If anyone else has any answers for how I could get the top 5 in a different way that would also be appreciated.


Solution

  • As you can see in other parts of the code, to access values in 2D array arrSize, you need 2 indexes in your case (and usually) i and j.

    You need to save both i and j after you find the highest number.

    if (max < arrSize[i][j]) {
       max = arrSize[i][j];
       indexI = i;
       indexJ = j;
    }
    

    and then

    arrSize[indexI][indexJ] = Integer.MIN_VALUE;
    

    As to why you got the error, arrSize[i] gets you a 1D array. It's still an array and you cannot set an array to an integer (Integer.MIN_VALUE). in Java represented as int[] in the error message.


    The algorithm could be improved, instead of using a single integer max for saving the highest value, you could use a maxArr of the same size as the number of highest numbers you want (in your case 5) and check against all of the numbers in maxArr using a for in place of

    if (max < arrSize[i][j]) {
       max = arrSize[i][j];
       index = i;
    }
    

    That would mean you could remove the index (or indexI and indexJ) and topmost for cycle (for (int p = 0; p < 5; p++)). But that's another topic, and one you should learn yourself.