나는 인터페이스를 구현하는 클래스가 있는데 배열에 요소를 두 번 이상 삽입하려고하면 첫 번째 삽입을 잊어 버리려고합니다. 나는이 것을 이해할 수 없다. 배열에 요소 삽입
public void insertElementAt(int index, E el)
throws IllegalArgumentException {
Object temp[] = new Object[data.length + 1];
for (int i = 0; i < data.length; i++) {
if (i == index){
temp[index] = el;
temp[i + 1] = data[i];
i++;
}
temp[i] = data[i];
}
data = temp;
if (index > data.length || index < 0) {
throw new IllegalArgumentException();
}
}
그런 다음 내 테스트는 그가 마지막 주장에 대신 처음의 null
를보고
@Test
public void testInsertToLeft() {
PriorityList<String> list = new ArrayPriorityList<String>();
list.insertElementAt(0, "First");
// Must shift array elements in this case
list.insertElementAt(0, "New First");
assertEquals("New First", list.getElementAt(0));
assertEquals("First", list.getElementAt(1));
}
인덱스가 필요합니다. 이전 배열과 새 배열에 하나씩 필요합니다. 그대로, 삽입으로 인해 하나의 요소를 건너 뛸 때 _both_ 배열을 건너 뜁니다. 추신 'List' 만 사용하십시오. –
'if' 블록의 끝에서'continue'가 필요합니다. – Admit
'데이터 '의 첫 번째 크기는 무엇입니까? –