Here is my code which works fine
The problem is, sometimes random numbers containing two maximum numbers such as 4, 10, 2, 10,6 is generated.With what i have, only one of the 10s is removed.How do i make sure both are removed?public class Arrays { public static void main (String[]args) { int maxNum= 0; int minNum = 100; int position=0; ArrayList <Integer> numbers = new ArrayList<Integer>(); int i; for (i=0; i<5;i++) { numbers.add((int)(Math.random()*10)+1); } Collections.sort(numbers); System.out.println(numbers); try { for (i=0;i<numbers.size();i++){ if (numbers.get(i) > maxNum) { position=i; maxNum = numbers.get(i); } } numbers.remove(position); //removes the index that contains the maximum number System.out.println("Maximum number: " + maxNum); System.out.println("Found max at index " + position); System.out.println(numbers); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); } }}