I am trying to write a method that will randomly add items to the ArrayList and then sort it based on the elements being added as you go, but it isn't working properly. I have posted my code below, but I don't know why it isn't working properly. Any help would be greatly appreciated.
public static void sortAndAdd(ArrayList<Integer> inL, int maxNumberOfElements) {
inL.clear();
inL.add(new java.util.Random().nextInt());
for (int i = 1; i < maxNumberOfElements - 1; i++) {
inL.add(new java.util.Random().nextInt(limit));
if (inL.get(i) > inL.get(i-1)) {
inL.set(i-1, inL.get(i));
inL.set(i, inL.get(i-1));
}
}
}