2012-03-04 36 views
0

我寫了一個SortedIntList類,它有一個add和get方法。移動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是outofbounds,但我不知道如何。

+0

顯然我錯誤地添加了代碼標記,我該如何解決這個問題? – 2012-03-04 03:33:31

+0

你會想看看常見問題,因爲你不使用[code] [/ code]標籤爲這個論壇。相反,您按下{}代碼按鈕,它將您的代碼縮進4個空格。 – 2012-03-04 03:36:17

+0

你發佈了'add'方法而不是'get'。 – talnicolas 2012-03-04 03:38:50

回答

0

使用Collections.sort()可能更容易,這種標準的Java方法會爲您的Collection排序。這樣你就不必自己處理排序,祝你好運!

+0

我會這樣做,但它的數據結構課程,所以我不允許:-( – 2012-03-04 03:57:02

+0

你可以添加「家庭作業」標籤,所以每個人都知道不能完全爲你解決問題。 – 2012-03-04 03:59:48

+0

爲什麼你不然後問你的課程的講師嗎?那就是你付錢的地方,對吧?祝你好運! – Wesley 2012-03-04 04:01:28

0

我看到一些問題。

首先,list.set(i+i, toBeShifted);應該可能是list.set(i+1, toBeShifted);。當您將7添加到列表中時,您的列表大小爲1.在for循環中,您將i初始化爲0(列表大小-1)。當你調用list.set(i + i,toBeShifted)時,你正在調用list.set(0,toBeShifted),所以並沒有實際移動這個值。其次,儘管你不會在添加9和7時碰到它,但你會在無限的while循環中結束。你永遠不會改變位置的價值。如果你添加了一個9,然後是一個更大的數字,你就會被洗淨。

+0

你是對的,我的意思是有我+ 1,而不是我+我,我糾正了無限循環(可能)的問題, m仍然沒有看到我的問題是什麼,即使修復後,我仍然得到相同的錯誤。 – 2012-03-04 04:03:35

+0

我可能已經偶然發現了一些東西。 a.set(1,0);這個thro線程「主」java.lang.IndexOutOfBoundsException異常:索引:1,大小:1這怎麼可能? – 2012-03-04 04:22:09

+0

列表。設置「用指定元素替換此列表中指定位置的元素」。當您試圖設置7時,您在位置1上沒有任何東西,因此無法替換。這是事故的原因。在進入for循環之前,您需要添加一個新項目到列表中(這會將其添加到列表的末尾)。然後當你做這個轉變的時候,這個數字就有了一個地方。您還需要將for循環更改爲'for(int i = list.size() - 2; i> = position; i - ){'因爲在此點之前列表的大小增加了一個。 – Tony 2012-03-04 04:25:01

1

閱讀Java Doc有幫助。顯然使用set()要求在你試圖覆蓋的位置已經有一個值。我需要使用add(position,value)來代替:-)

0

您不能使用列表的set()添加到列表中:例如,如果您嘗試將索引1處的內容設置爲大小列表中的內容1,你會得到一個IndexOutOfBoundsException

基本上,您首先需要add