NPE自己实现的LinkedList(Java)

时间:2017-02-13 18:49:08

标签: java linked-list

我是编程的初学者,我需要编写某种自己的LinkedList,但只能使用@NgModule({ … imports: [ BrowserModule, FormsModule, HttpModule, RouterModule.forRoot(routes,{ useHash: true }) ], … }) 方法和Iterator的add(E element)hasNext()。这是我的代码:

next()

但是当我开始测试添加(E值)......

  public class LinkedArray<E> implements Iterator<E> {

        private int size = 0;

        private int current = 0;

        private Node<E> first;

        private Node<E> last;

        private Objects[] objects = new Objects[10];

        public void add(E value) {
            Node<E> element = new Node<E>(last, value, null);
            if (last != null) {
                element.next = element;
            } else {
                first = element;
            }
            last = element;
            size++;
        }

        @Override
        public boolean hasNext() {
            boolean result = false;
            try {
                if (objects[current + 1] != null) {
                    result = true;
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                result = false;
            }
            return result;
        }

        @Override
        public E next() {
            E result;
            try {
                current++;
                result = (get(current - 1));
            } catch (ArrayIndexOutOfBoundsException a) {
                throw new NoSuchElementException("No more elements in list.");
            }
            return result;
        }

   public E get(int position) throws NullPointerException {
        Object result;
    if (this.objects[position] != null) {
        result = this.objects[position];
    } else {
        throw new NullPointerException("Position is empty.");
    }
    return (E) result;
}


        private class Node<E> {

            private E element;

            private Node<E> next;

            private Node<E> prev;

            Node(Node<E> prev, E element, Node<E> next) {
                this.element = element;
                this.next = next;
                this.prev = prev;
            }
        }
    }

......我只收到错误。问题是什么,为什么我弄错了?

2 个答案:

答案 0 :(得分:2)

您已明确抛出自己的NPE。

public E get(int position) throws NullPointerException {
    Object result;
    if (this.objects[position] != null) {
        result = this.objects[position];
    } else {
        throw new NullPointerException("Position is empty.");
    }
    return (E) result;
}

如果您想关注the get() method contract of a List,Javadoc会说这个

  

<强>抛出:
  IndexOutOfBoundsException - 如果索引超出范围(index < 0 || index >= size())

因此,因为你没有&#34;引用&#34;任何null,而只是在你的数组为空的情况下转到return null,然后抛出另一个异常。

public E get(int position) throws IndexOutOfBoundsException {
    if (position < 0 || position >= this.objects.length) {
        throw new IndexOutOfBoundsException();
    }
    return (E) this.objects[position];
}

注意:Iterator类通常不会使用E get()方法。只需hasNext()next()

因此,您不应以next()需要调用get()的方式实施您的课程。

你也不需要try-catch。您已经知道position何时使用if语句超出范围。

答案 1 :(得分:0)

您的get(...)函数从objects数组中读取对象。 您永远不会设置此数组的内容,因此如果position小于10,它将始终为null并导致您遇到的NPE。

您似乎已经调整了基于数组的列表,但只更改了add方法。所有其他方法都与空objects数组交互。

相关问题