add 및 get 메서드가있는 SortedIntList 클래스를 작성했습니다.ArrayList 이동
나는 다음과 같은 네 가지 방법을 호출 오전 :
SortedIntList mySortedIntList = new SortedIntList();
mySortedIntList.add(9);
mySortedIntList.add(7);
System.out.println("0 is :"+mySortedIntList.get(0));
System.out.println("1 is :"+mySortedIntList.get(1));
내 GET 및 메소드를 추가는 다음과 같습니다
public void add(Integer newValue) {
int position = 0;
while(position < list.size()){
int currentPosValue = list.get(position);
if(newValue <= currentPosValue){
for(int i=list.size()-1; i>=position; i--){
int toBeShifted = list.get(i);
list.set(i+1, toBeShifted);
}
list.set(position, newValue);
return;
}
position++;
}
list.add(newValue);
}
public int get(int i) throws IndexOutOfBoundsException {
// Postcondition: If i < 0 or i >= size() throws
// IndexOutOfBoundsException, otherwise returns the value
// at position i of this IntList
if (i < 0 || i >= list.size()) {
throw new IndexOutOfBoundsException("SortedIntList.get");
} else {
return ((Integer) list.get(i)).intValue();
}
}
public int get(int i) throws IndexOutOfBoundsException {
// Postcondition: If i < 0 or i >= size() throws
// IndexOutOfBoundsException, otherwise returns the value
// at position i of this IntList
if (i < 0 || i >= list.size()) {
throw new IndexOutOfBoundsException("SortedIntList.get");
} else {
return ((Integer) list.get(i)).intValue();
}
}
내가 종이에 그것을 밖으로 작성한, 그것은 논리적 인 것 같다,하지만 코드가 폭발하다 :
System.out.println("1 is :"+mySortedIntList.get(1))
줄은 분명히 1이지만, 나는 어떻게 보이지 않는다.
분명히 코드 태그를 잘못 추가했는데 어떻게 해결할 수 있습니까? –
이 포럼에서는 [code] [/ code] 태그를 사용하지 않으므로 FAQ를보고 싶을 것입니다. 대신 {} 코드 버튼을 누르면 코드 4 칸을 들여 쓰게됩니다. –
당신은'get'이 아닌'add' 메소드를 게시했습니다. – talnicolas