我自己的ArrayBasedList类上的remove()方法删除了错误的索引

时间:2016-04-28 02:32:04

标签: java arraylist

我终于正确地弄清楚了我的插入,但几天来我一直在研究删除方法,它只是不会给我想要的输出。我的删除方法只删除最后一个索引而不是我指定的位置。我的方法必须是一个布尔值,其int position参数将它带到方法中。我必须做什么才能让我的代码在给定的指定位置参数中删除对象,而不是最后一个索引。 我的代码如下......

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;
    }
}

预期的输出在我的重新打印列表上方的print语句中指定...我得到的输出在最后一个索引处被删除,而不是在指定位置。

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 个答案:

答案 0 :(得分: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;        
}