在泛型链表类中开发添加到索引方法



我在将 add 方法实现到在给定索引处添加一个值的通用链表类时遇到了一些问题。但是,在通过测试类运行它时,我无法正确执行此操作,我收到以下错误:

1) t31AddtoEmptyList(LinkedListTest$Add2Test)
java.lang.IndexOutOfBoundsException
2) t32AddtoThreeItemList0(LinkedListTest$Add2Test)
java.lang.AssertionError: four element list has wrong size. expected:<4> but was:<3>
3) t35AddtoThreeItemList3(LinkedListTest$Add2Test)
java.lang.IndexOutOfBoundsException

这是我在索引处添加的方法:

public void add(int index, E value){
    if (index < 0 || index >= this.size) {
        throw new IndexOutOfBoundsException();
    //if empty add to front
    }else if(index == 0) {
        front = new ListNode(value, front);
    // if index is last element add to front
    }else if(index == size){
        add(value);
    }else {
        ListNode current = front;
        for (int i = 0; i < index - 1; i++){
            current = current.next;
        }
        current.next = new ListNode(value, current.next);
    }
    this.size++;
}

和我的全班:

public class LinkedList<E>{
    int size;
    private class ListNode{
        E data;
        ListNode next;
        ListNode(E data) {
            this.data = data;
            next = null;
        }
        ListNode(E data, ListNode next) {
            this.data = data;
            this.next = next;
        }
    }
    private ListNode front;
    private ListNode end;
    // Constructor
    // Construct an empty LinkedList object.
    public LinkedList(){
        this.size = 0;
        this.front = null;
    }
    // Return the size (number of items) in this LinkedList.
    public int size(){
        return size;
    }
    // Return true if this LinkedList has no items.
    // Return false if the size is greater than zero.
    boolean isEmpty(){
        return size() == 0;
    }
    //  Add the given element, value, to the end of the list.
    // appends
    public void add(E value){
        if (front == null) {
            front = new ListNode(value);
        } else {
            ListNode current = front;
            while (current.next != null) {
                current = current.next;
            }
            current.next = new ListNode(value);
        }
        this.size++;
    }
    // Add the given element, value, to the list at the given index.
    // After this operation is complete, get(index) will return value.
    // This operation is only valid for 0 <= index <= size().
    public void add(int index, E value){
        if (index < 0 || index >= this.size) {
            throw new IndexOutOfBoundsException();
        //if empty add to front
        }else if(index == 0) {
            front = new ListNode(value, front);
        // if index is last element add to front
        }else if(index == size){
            add(value);
        }else {
            ListNode current = front;
            for (int i = 0; i < index - 1; i++){
                current = current.next;
            }
            current.next = new ListNode(value, current.next);
        }
        this.size++;
    }
    /*
    // prepend helper method
    //prepend
    public void prepend(int data){
        if (front == null){
            end = new ListNode();
            front = end;
        }
        else {
            front = new ListNode(data,front);
        }
        size++;
    }
    */
    // Return the element of the list at the given index.
    // This operation is only valid for 0 <= index < size().
    // This operation does not modify the list.
    public E get(int index){
        // check for index out of bounds
        if (index < 0 || index >= this.size) {
            throw new IndexOutOfBoundsException();
        }
        // returns char at index
        ListNode curr = front;
        for (int i = 0; i < index; i++) {
            curr = curr.next;
        }
        return curr.data;
    }
} // end class

我的测试类 https://gist.github.com/Silverfin13/e22d061c2daf0d3a7013

我感谢任何帮助,谢谢

if (index < 0 || index >= this.size) {
    throw new IndexOutOfBoundsException();

在这里index >= this.size public void add(int index, E value)顶部的条件是问题所在我想应该是index > this.size

为什么?如果要在最后一个位置的节点处添加index将等于当前size并且有效。

你也必须else if(index == size)这个条件,所以这意味着你想允许index等于size但在顶部你为此抛出了一个new IndexOutOfBoundsException();


您在大小维护方面还有另一个问题。

public void add(int index, E value)此方法,请增加任何有效插入this.size++;的大小。 但以防index==size

else if(index == size){
        add(value);

您调用 public void add(E value) 方法,该方法也会在操作结束时增加大小,以防index==size this.size增加两次。

相关内容

  • 没有找到相关文章

最新更新