2015-01-05 4 views
4

왜 이것이 반복적으로 루핑되는지 알 수 없습니다.Java 반복기 무한 루프

public void DLCCheck(IconSet iconSet) { 
    Log.d(TAG, "Got dlc check. Looking to see if we need to remove any notes from the current list."); 
    int foundCount = 0; 
    for(Iterator<Item> i = mItemList.iterator(); i.hasNext();) { 
     if(i instanceof NoteItem && ((NoteItem) i).getIconSet() == iconSet) { 
      i.remove(); 
      foundCount++; 
     } 
    } 
    Log.d(TAG, "Finished searching. Found " + foundCount + "notes in the current list to delete."); 
    //notifyDataSetChanged(); 
    //EventBus.getDefault().post(new MoveNoteListOut()); 
} 

hasNext가 false를 반환 할 때 반복을 중지하면 안됩니까? 이 목록에있는 항목은 6 개 뿐이지 만 계속 반복됩니다.

+0

이 실제로 도움이되었다. 왜이 주제에서 벗어난거야 ?? – dan

답변

11

당신은 결코 i.next()으로 전화하지 않을 것입니다. 또한 iinstanceof Iterator이므로 i instanceof NoteItem은 결코 true이 아닙니다. i.next()에있는 데이터를 읽고 해당 조건을 조건으로 평가해야합니다. 코드가 어떻게해야에

이 :

for(Iterator<Item> i = mItemList.iterator(); i.hasNext();) { 
    Item item = i.next(); 
    if(item instanceof NoteItem && ((NoteItem) item).getIconSet() == iconSet) { 
           //here ---------------------------^^ 
           //not sure what type returns getIconSet 
           //but if it's not a primitive then you should use equals 
     i.remove(); 
     foundCount++; 
    } 
} 
+0

고마워요! 왜 원시 메소드가 아니라면 equals 메소드를 사용해야합니까? – AnnonAshera

+0

객체 참조에서'=='은 참조의 동등성을 검사하기 때문에 'equals'는 참조 상태의 동등성을 평가합니다. 이것은 [Java에서 문자열을 어떻게 비교합니까?] (http://stackoverflow.com/q/513832/1065197) –

+0

아, 예. 고맙습니다. – AnnonAshera