I want to search through an ArrayLst and delete any entries that are the same.
For example if my list was: apple, orange, banana, pear, peach, orange,
then "orange" would be deleted (both occurances).
Naively, I tried:
for(String word : userlist){
for(String otherword : userlist){
...
}
}
where I wrote how to .remove(lastIndexOf(userword)) if it equals word and their indexes are different.
This led to exception after exception, and I quickly realized I was manipulating a list while iterating through it which was making it all go wrong.
So 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. Notably ConcurrentModificationException. After tweaking it I can't get, what in my head should be a fairly easy process, to work in Java. Please help.