Sort an array in Java

后端 未结 17 962
傲寒
傲寒 2020-11-22 04:53

I\'m trying to make a program that consists of an array of 10 integers which all has a random value, so far so good.

However, now I need to sort them in order from l

17条回答
  •  情深已故
    2020-11-22 05:30

    Here is how to use this in your program:

    public static void main(String args[])
    {
        int [] array = new int[10];
    
        array[0] = ((int)(Math.random()*100+1));
        array[1] = ((int)(Math.random()*100+1));
        array[2] = ((int)(Math.random()*100+1));
        array[3] = ((int)(Math.random()*100+1));
        array[4] = ((int)(Math.random()*100+1));
        array[5] = ((int)(Math.random()*100+1));
        array[6] = ((int)(Math.random()*100+1));
        array[7] = ((int)(Math.random()*100+1));
        array[8] = ((int)(Math.random()*100+1));
        array[9] = ((int)(Math.random()*100+1));
    
        Arrays.sort(array); 
    
        System.out.println(array[0] +" " + array[1] +" " + array[2] +" " + array[3]
        +" " + array[4] +" " + array[5]+" " + array[6]+" " + array[7]+" " 
        + array[8]+" " + array[9] );        
    
    }
    

提交回复
热议问题