-1

I am trying to sort a static array of Ticket objects into two different arrays based on their dates and Class. These two arrays will be used as data to populate two different list views. I only want to add one Ticket per Class object to each array. I get an error

    ArrayList<Ticket> myUpcomingTickets = new ArrayList<>();
    ArrayList<Ticket> myPastTickets = new ArrayList<>();

    for (Ticket t : Ticket.getTickets()) {
        if (t.getClass().getDate().after(date)) {
            if(myUpcomingTickets.isEmpty()){
                myUpcomingTickets.add(t);
            }else {
                for(Ticket t1: myUpcomingTickets) {
                    if (!t.getClass().getId().equals(t1.getClass().getId())) {
                        myUpcomingTickets.add(t);
                    }
                }
            }
        } else {
            if(myPastTickets.isEmpty()){
                myPastTickets.add(t);
            } else {
                for(Ticket t2: myPastTickets) {
                    if (!t.getClass().getId().equals(t2.getClass().getId())) { //Add if not already there
                        myPastTickets.add(t);
                    }
                }
            }
        }
    }

Error Show in Logcat java.util.ConcurrentModificationException

Ali
  • 3,346
  • 4
  • 21
  • 56
Ron95
  • 136
  • 1
  • 2
  • 13
  • 1
    https://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property – ADM Mar 02 '18 at 13:30
  • you googled the error of course, so you already know what the problem is. What is your question? – Tim Mar 02 '18 at 13:34

2 Answers2

1

There are a few things wrong with this. The error is caused by this

for(Ticket t2: myPastTickets) {
                    if (!t.getClass().getId().equals(t2.getClass().getId())) { //Add if not already there
                        myPastTickets.add(t);
                    }
                }

you are not allowed to add or remove from a list you are currently looping through.

In addition this method would be very slow because of the nested loops you have.

My suggesting would be 1) extend your objects hash and equals to make your object easier to work with

public class Ticket {
 @Override
    public int hashCode() {
        return getId().hashCode();
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Ticket other = (Ticket ) obj;
        return other.getId() == this.getId();
    }
}

and then you can do this:

ArrayList<Ticket> myUpcomingTickets = new ArrayList<>();
    ArrayList<Ticket> myPastTickets = new ArrayList<>();

    for (Ticket t : Ticket.getTickets()) {
        if (t.getClass().getDate().after(date)) {
            if(!myUpcomingTickets.contains(t)){
                myUpcomingTickets.add(t);
            }
        } else {
            if(!myPastTickets.contains(t)){
                myPastTickets.add(t);
            }
        }
    }

and if you dont need the lists to be in an order i would suggest using a hashmap instead of arraylist to speed this up

Tomer Shemesh
  • 10,278
  • 4
  • 21
  • 44
0

You are modifying the lists (myUpcomingTickets, myPastTickets) while iterating them!
What you could do:
1. If not implemented yet, implement the equals and hashcode for Ticket object
2. Use list.contains(Obj) instead of using the for loop

E.g:

if(!myUpcomingTickets.contains(t)){
                myUpcomingTickets.add(t);
}
Alex
  • 3,382
  • 2
  • 32
  • 41