I want to search through ArrayLst and delete any entries that are the same.
For example, if my list was: apple, orange, banana, pear, peach, orange,
then the "orange" will be deleted (both events).
Naively, I tried:
for(String word : userlist){ for(String otherword : userlist){ ... } }
where I wrote as .remove (lastIndexOf (userword)) if it is equal to the word and their indices are different.
This led to an exception after the exception, and I quickly realized that I was manipulating the list, iterating through it, which made it all go wrong.
So, we decided to make a copy of the list
ArrayList<String> copylist = userlist; for(String word : copylist){ for(String otherword : copylist){ if(word.equalsIgnoreCase(otherword) && copylist.lastIndexOf(word)!=copylist.lastIndexOf(otherword)){ userlist.remove(userlist.lastIndexOf(word)); userlist.remove(userlist.lastIndexOf(otherword)); } } }
SO I tried this and it had similar problems. In particular, ConcurrentModificationException. After setting it up, I cannot get what in my head should be a fairly simple process to work in Java. Please, help.
java arraylist
user485498
source share