I've been getting this exception in my program:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
at java.util.LinkedList$ListItr.next(Unknown Source)
at fenn20.CarLists.main(CarLists.java:46)
It seems to be caused by last couple lines in the program(.hasPrevious and previous), but that doesn't seem to make sense since .hasNext works earlier in the code.
public static void main(String[] args) {
ArrayList <Car> cararray1 = new ArrayList <Car>();
cararray1.add(new Car("Toyota", "Corolla", 10000));
cararray1.add(new Car("Honda", "Civic", 5000));
cararray1.add(new Car("Honda", "Accord", 12500));
cararray1.add(new Car("Mitsubishi", "Outlander", 20000));
cararray1.add(new Car("Mitsubishi", "Lancer", 15000));
cararray1.add(new Car("Toyota", "Camry", 7500));
for (int i = 0; i < cararray1.size(); i++) {
System.out.println(cararray1.get(i));
}
System.out.println();
System.out.println("Most Expensive Car: " + Collections.max(cararray1, new CarComparator()));
LinkedList<Car> cararray2 = new LinkedList <Car>(cararray1);
cararray2.add(4, new Car("Hyundai", "Elantra", 3000));
System.out.println("Added New Car: " + cararray2.get(4));
Collections.sort(cararray2, new CarComparator());
System.out.println();
ListIterator<Car> lit = cararray2.listIterator();
System.out.println("In Increasing Order:");
while(lit.hasNext()){
System.out.println(lit.next());
}
System.out.println();
System.out.println("Removed First Car: " + cararray2.get(0));
cararray2.remove(0);
System.out.println("Removed Last Car: " + cararray2.get(5));
cararray2.remove(5);
System.out.println();
System.out.println("In Decreasing Order:");
while(lit.hasPrevious()){
System.out.println(lit.previous());
}
}
}