java - 尝试启动这个链表

标签 java

第二次编程课 因此,我们的任务是使用一个链表,从头开始构建每个方法。 好吧,我从前天开始,遇到了空指针异常,我想稍后解决它并继续。 好吧,在将我的程序削减到没有找到罪魁祸首之后,我留下的代码应该像从我们实验室复制的那样工作(有效)。 如果你们认为自己能弄清楚为什么我在 add 方法上遇到空指针异常,我会非常感激,并看看我是否正确执行了第二个构造函数。如果我能在这方面获得一些牵引力来开始,那么事情就会变得更容易,但我什至无法开始。

您会注意到分配了空白方法,一旦我可以让我的构造函数+添加方法工作,我就会使用它们

我的代码:

import java.util.Collection;
import java.util.Iterator;

/**
 * Created by hhhh on 11/2/2014.
 */
public class Lset<R> implements Set151Interface<R> {

    private Node    head;
    private int     length;

    /**In the first (following) constructor im trying to re use code and call my clear method.
     *Should save space and make code look cleaner.
     */
    public Lset(){
        clear();
    }

    public Lset(Collection<? extends R> list){
        this();
        for (R object : list) {
            add(object);
        }


    }

    /**
     * Copied from Lab7, this add method checks to see if there are more nodes than just the head.
     * After the check if false, creates a new node and adds it to the end of the list.
     * @param entry
     * @return
     */
    @Override
    public boolean add(R entry) {
        Node newNode = new Node(entry);

        // empty list is handled differently from a non-empty list
        if (head.next == null) {
            head = newNode;
        } else {
            Node lastNode = getNodeAt(length - 1);
            lastNode.next = newNode;
        }
        length++;
        return true;
    }

    @Override
    public void clear() {
        this.length     = 0;
        this.head       = null;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @Override
    public Iterator<R> iterator() {
        return null;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends R> c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return false;
    }

    @Override
    public int size() {
        return length;
    }

    @Override
    public Object[] toArray() {
        return null;
    }

    @Override
    public <T> T[] toArray(T[] array) {
        return null;
    }


    /**
     * Code used in Lab 7, getNodeAt uses the length field and starts at head to traverse array and arrive at the
     * position desired.
     * @param position
     * @return
     */
    private Node getNodeAt(int position) {
        assert !isEmpty() && (position >= 0) && position < length;

        Node cNode = head;

        for (int i = 0; i < position; i++)
            cNode = cNode.next;

        assert cNode != null;
        return cNode;
    }


    public String toString(){
        String arrayString = "<";
        for(int i = 0; i < length; i++){
            String two = getNodeAt(i).toString();
            arrayString += two;
            if(i <= (length - 2)){
                two = ", ";
                arrayString += two;
            }

        }
        arrayString += ">";

        return arrayString;
    }

    //TODO comment better
    public class Node {
        /** Reference to the data */
        public R data;
        /** Reference to the next node is in the list */
        public Node next;

        /**
         * Sets the data for this node.
         * @param data data to be carried by this node.
         */
        public Node(R data) {
            this.data = data;
            this.next = null;
        }

        /**
         * Sets the data for the node and assigns the next node in the list.
         * @param data data to be carried by this node.
         * @param nextNode next node in the list.
         */
        public Node(R data, Node nextNode) {
            this.data = data;
            this.next = nextNode;
        }
        /**
         * Returns just the data portion of the node.
         * @return The data portion of the node.
         */
        public R getData() {
            return this.data;
        }
        /**
         * Modified just the data portion of the node.
         * @param data new data to be contained within the node.
         */
        public void setData(R data) {
            this.data = data;
        }

        /**
         * What node does this node point to.
         * @return the node that this node points to or null if it does not
         * point anywhere.
         */
        public Node getNextNode() {
            return this.next;
        }

        /**
         * Change the node that this node points to.
         * @param nextNode a new node for this node to point to.
         */
        public void setNextNode(Node nextNode) {
            this.next = nextNode;
        }

        /**
         * Display the state of just the data portion of the node.
         */
        public String toString() {
            return this.data.toString();
        }
    }



}

这是 main 中杀死它的方法

private void testConstruction() {
        System.out.println("\nTesting Constructor");
        System.out.print("----------------------------------------");
        System.out.println("----------------------------------------");

        Set151Interface s = makeSet();
        //added
        s.add("Butterfinger");
        test(s.size() == 0,

                "size() should return 0: " + s.size());
        test(s.toString().equals("<>"),
                "toString returns \"<>\": " + s.toString());


        ArrayList<String> temp = new ArrayList<String>();
        temp.add("Butterfinger");
        temp.add("Milky Way");
        temp.add("Kit Kat");
        temp.add("Three Muskateers");

        Set151Interface s3 = makeSet(temp);
            test(s3.size() == 4,
                "size should return 4: " + s3.size());
        test(s3.toString().equals("<Butterfinger, Milky Way, Kit Kat, Three Muskateers>"),
                "toString should return\n        "+
                        "\"<Butterfinger, Milky Way, Kit Kat, Three Muskateers>\":\n       "
                        + s3.toString());

    }

一旦 Butterfinger 尝试添加,我就会收到指向此行的空指针异常 if (head.next == null) {

最佳答案

您刚刚声明了私有(private)节点头;并且它不接受任何分配的值。所以编译器会抛出 NPE

关于java - 尝试启动这个链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26776703/

相关文章:

java - GlassFish 3.1 中客户端的 ctx.lookup() 时出现 CommunicationException

java - AS 2.3.3、Gradle 3.3、Java 1.8 无法解析 java.time.format.DateTimeFormatter 的导入;

java - super 强大的 Android JNI : understanding "JNI(jintArray ..." format

java - 在 Spring Batch 中使用 stepExecution

java - 如何在本地使用java使用spark连接到Google大查询?

java - 带图标的菜单项溢出?

java - 避免内存不足错误

java - void 方法递归错误

java - 我需要帮助在 Java 的 MySQL DateTime 范围内进行选择

java - 防止Spring的RestTemplate为multipart/form-data中的每个参数添加 header