do{
System.out.println("inside do");
for (int i = 0; i < i2; i++) {
String s2 = m_area.m_items.get(i).returnName();
if (s2.contains(s)) {
itemexist_check = true;
player.addItem(m_area.m_items.get(i));
m_area.m_items.remove(i);
} else {
//do nothing
}
}
itemexist_check = true;
}while(itemexist_check == false);
는 행복하게 "player.addItem (m_area.m_items.get (i)를)"수행, 실행되지만, 그것은 (에 "m_area.m_items.remove을 얻을 때 i) "(m_items는 LinkedList 임),"OutOfBoundsException : Index 1, Size 1 "을 발생시킵니다. "i"값을 인쇄 할뿐만 아니라 LinkedList의 크기를 인쇄하여 "m_area.m_items.remove (i)"이전에 LinkedList의 크기가 2이고 " 나는 그것이 0 인 이유를 이해했다. LinkedList에는 요소 0이 없습니다. 왜 내가 "player.addItem (m_area.m_items.get (i))"에 예외를 던지지 않았는지 모르겠습니다. 뭔가 이상한 것이 있습니다. 제발 도와주세요. 자바 : 이상한 OutOfBoundsException이 문을
는
오. 분명히 LinkedList.get은 인덱스가 0보다 작을 때 예외를 throw하지만 LinkedList.remove는 인덱스가 0 일 때 예외를 throw합니다. 내 실제 질문은 어떻게하면 코드를 수정할 수 있습니까? – Blackvein
질문이 명확하지 않습니다. 예를 들어. 'i2'가 가리키는 점은 무엇입니까? –
올바르지 않습니다. [LinkedList.get] (http://download.oracle.com/javase/7/docs/api/java/util/LinkedList.html#get%28int%29) throws'IndexOutOfBoundsException' - 인덱스가 범위를 벗어난 경우 –