2016-04-28 42 views
0

我終於想出了我的插入正確,但幾天來一直在工作的刪除方法,它只是不會給我我想要的輸出。我的刪除方法是隻刪除最後一個索引,而不是在我指定的位置。我的方法必須是一個帶有int位置參數的布爾值,並將它帶到方法中。我必須做什麼才能讓我的代碼在指定的位置參數中移除一個對象,而不是最後一個索引。 我的代碼如下......,預計在打印語句中指定我上面的列表轉載我自己的ArrayBasedList類的remove()方法刪除錯誤索引

public class ArrayBasedList<T> implements ListInterface<T>{ 
    private int MAX_ITEMS = 20;  // Maximum Number of items the items array can hold. 
    private Item<T>[] items;  // Array that will hold items of type T 
    private int count;   // Number of valid items in the array 

    ArrayBasedList(){ 
     items = new Item[MAX_ITEMS]; 
     count = 0; 
    } 

    public void add(T item) { 
     if(isFull()) return; 
     items[count] = new Item(item); 
     count++; 
    } 

    public void insert(int position, T item) { 
     for(int i = count-1; i >= 0; i--) { 
      if(position > count || position > MAX_ITEMS) return; 
      if(items[position] == items[count]){ 
       count++; 
       add(item); 
       items[i].data = items[i+1].data; 
      } 
      items[position].data = item; 
     } 
    } 

    public T get(int position) { 
     if(position < 0 || position >= count) { 
      throw new RuntimeException("Index out of Bounds. "); 
     } 
     return items[position].data; 
    } 


    public boolean set(int position, T item) { 
     if(position > count || position > MAX_ITEMS) return false; 
     items[position].data = item; 
     return true; 
    } 

//METHOD THAT IS GIVING ME AN ISSUE 
    public boolean remove(int position) { 
     if(isEmpty() || position > count || position > MAX_ITEMS) return false; 
     for(int i = count-1; i < position; i--) { 
      if(items[i] == items[position]) { 
       items[i].data = items[i-1].data; 
      } 
      items[i] = items[position]; 
     } 
     count--; 
     return true; 
    } 

    public boolean isFull() { 
     if(count >= MAX_ITEMS) return true; 
     return false; 
    } 

    public boolean isEmpty() { 
     if(count == 0) return true; 
     return false; 
    } 

    //Prints my List 
    public void printAll() { 
     for(int i = 0; i < count-1; i++) { 
      System.out.println(items[i].toString()); 
     } 
    } 

    //Counts number of objects in list 
    public int size() { 
     return count; 
    } 
} 

輸出...輸出我得到的是最後一個索引刪除,而不是在指定的位置。

OUTPUT: 
The list is as follows: 
[1.0x2.0x3.0] 
[2.0x3.0x4.0] 
[3.0x4.0x5.0] 
[4.0x5.0x6.0] 
[5.0x6.0x7.0] 
[6.0x7.0x8.0] 
[7.0x8.0x9.0] 
[8.0x9.0x10.0] 
[9.0x10.0x11.0] 
[10.0x11.0x12.0] 

Size of my list is 11 

Attempting to remove item at location 0. 
The current list is as follows: 
[1.0x2.0x3.0] 
[2.0x3.0x4.0] 
[3.0x4.0x5.0] 
[4.0x5.0x6.0] 
[5.0x6.0x7.0] 
[6.0x7.0x8.0] 
[7.0x8.0x9.0] 
[8.0x9.0x10.0] 
[9.0x10.0x11.0] 

Size of my list is 10 

Attempting to remove item at location 5. 
The current list is as follows: 
[1.0x2.0x3.0] 
[2.0x3.0x4.0] 
[3.0x4.0x5.0] 
[4.0x5.0x6.0] 
[5.0x6.0x7.0] 
[6.0x7.0x8.0] 
[7.0x8.0x9.0] 
[8.0x9.0x10.0] 

Size of my list is 9 

Attempting to insert a cube 100*200*300 at location 9. 
The current list is as follows: 
[1.0x2.0x3.0] 
[2.0x3.0x4.0] 
[3.0x4.0x5.0] 
[4.0x5.0x6.0] 
[5.0x6.0x7.0] 
[6.0x7.0x8.0] 
[7.0x8.0x9.0] 
[8.0x9.0x10.0] 
[10.0x11.0x12.0] 
[100.0x200.0x300.0] 

Size of my list is 11 

Attempting to insert a cube 100*200*300 at location 3. 
The current list is as follows: 
[1.0x2.0x3.0] 
[2.0x3.0x4.0] 
[3.0x4.0x5.0] 
[100.0x200.0x300.0] 
[5.0x6.0x7.0] 
[6.0x7.0x8.0] 
[7.0x8.0x9.0] 
[8.0x9.0x10.0] 
[10.0x11.0x12.0] 
[100.0x200.0x300.0] 

Size of my list is 11 

Attempting to set the value at location 5 to a cube 10*11.2*20.9. 
The current list is as follows: 
[1.0x2.0x3.0] 
[2.0x3.0x4.0] 
[3.0x4.0x5.0] 
[100.0x200.0x300.0] 
[5.0x6.0x7.0] 
[10.0x11.2x20.9] 
[7.0x8.0x9.0] 
[8.0x9.0x10.0] 
[10.0x11.0x12.0] 
[100.0x200.0x300.0] 

The current size of the list is: 11 
The value at position 3 is: [100.0x200.0x300.0] 
+1

您是否已經完成了IDE調試器中的代碼?在尋求幫助之前,一定要這樣做,98%的時間你會自己發現問題,並在過程中學到一些東西。更有意義。 –

+0

@JimGarrison我曾嘗試調試它。通常這確實解決了我的問題,我完全同意它幫助我瞭解過程,但是在調試過兩次並跟蹤一次之後,我不知道爲什麼我無法找到問題,而且我的任務昨天到期,所以我只是我不想放棄任何進一步的觀點。但是,感謝您的意見我總是欣賞任何建設性的批評。 – Kassie

回答

1

試試這種方法。

public boolean remove(int position) { 
    if(isEmpty() && position>count && position > MAX_ITEMS){ 
     return false 
    } 
     for (int i = 0; i < count; i++) { 
      if (items[i] == position) { 
       --count; 
       for (; i < count; ++i) { 
        items[i].data = items[i + 1].data; 
       } 
       return true; 
      } 
     } 
    return false;   
} 
+0

非常感謝!我改變了一行...從{if(items [i] == position)}到{if(items [i] == items [position])}。 – Kassie