ArrayLst를 검색하여 동일한 항목을 모두 삭제하려고합니다. 예를 들어Java에서 ArrayList 수정
내 목록이 있다면 : 사과, 오렌지, 바나나, 배, 복숭아, 오렌지,
다음 "오렌지"가 삭제 될 것입니다 (두 차례 나오는).
순진, 내가 시도 : 나는 단어가 동일한 경우 (lastIndexOf에서도 (userword)) .remove하는 방법을 썼다
for(String word : userlist){
for(String otherword : userlist){
...
}
}
이 및 자신의 인덱스가 다릅니다.
이것은 예외 후에 예외로 이어지고, 나는 그것이 잘못되어 가고있는 것을 반복하면서 목록을 조작하고 있다는 것을 빨리 알게되었다.
그래서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));
}
}
}
그래서 난이 시도 목록의 복사본을 만들하기로 결정하고, 비슷한 문제가 있었다. 특히 ConcurrentModificationException. 내가 조정할 수 없으면, 자바에서 작업하기 위해서는 머리 속에있는 것이 꽤 쉬운 과정이어야한다. 도와주세요.
절대로 잠들지 않습니까? :( – musiKk
@ musiKk : 참조 http://meta.stackexchange.com/questions/555/ –
@MusiKk 또한 http://meta.stackexchange.com/questions/9134/jon-skeet-facts를 참조하십시오. –