【问题标题】:Null Pointer Exception with Singly Linked List to hashtable Java带有指向哈希表 Java 的单链表的空指针异常
【发布时间】:2015-03-06 22:33:52
【问题描述】:

这是我们无法修改的初始类

public class SLL {
    public class Node {
        private int data;
        private Node next;

        public Node() {
            data = 0;
            next = null;
        }

        public Node(int newData, Node linkValue) {
            data = newData;
            next = linkValue;
        }

        public int getData() {
            return data;
        }

        public Node getLink() {
            return next;
        }
    }// End of Node inner class

    private Node head;

    public SLL() {
        head = null;
    }

    public void addToStart(int itemData) {
        head = new Node(itemData, head);
    }

    public boolean contains(int item) {
        return (find(item) != null);
    }

    /**
     * Finds the first node containing the target item, and returns a reference
     * to that node. If target is not in the list, null is returned.
     */
    public Node find(int target) {
        Node position = head;
        int itemAtPosition;
        while (position != null) {
            itemAtPosition = position.data;
            if (itemAtPosition == target) {
                return position;
            }
            position = position.next;
        }
        return null; // target was not found
    }

    public void outputList() {
        Node position = head;
        while (position != null) {
            System.out.print(position.data + "  ");
            position = position.next;
        }
        System.out.println();
    }
}

这是我们应该完成的 Set 类以使 Tester 工作,我的 add 方法不断收到空指针异常,但是,它几乎与我在其他代码中看到的一样,包括我们的文本书。任何见解都将不胜感激,因为我的讲师已经预先制作了 powerpoint,并且没有解释任何内容或向寻求帮助的学生提供任何建议。

public class Set {
    private SLL[] hashArray; // DO NOT MODIFY THIS LINE
    private int size = 10; // DO NOT MODIFY THIS LINE

    // DO NOT MODIFY THIS METHOD
    public Set() {
        hashArray = new SLL[size];
    }

    // DO NOT MODIFY THIS METHOD
    private int computeHash(int s) {
        return s % size;
    }

    // COMPLETE BELOW

        public void add(int x)
    {
        int hash = computeHash(x);  // Get hash value
        SLL list = hashArray[hash];
                if (!list.contains(x))
        {
            // Only add the target if it's not already
            // on the list.
            list.addToStart(x);/*replaced hashArray[hash] with list*/
        }               
        }

        public void output( )
        {
            System.out.println("I will work on this later");
        }     
}

最后,测试者...

public class Tester{

    // Have this method to display your name, instead.
    static void displayName(){
        System.out.println("Program written by Tony.\n");
    }

    // DO NOT MODIFY THE MAIN METHOD
    public static void main(String[] args){
        displayName();
        Set set1 = new Set();
        Set set2 = new Set();

        set1.add(3);
        set1.add(3);
        set1.add(13);
        set1.add(23);
        set1.add(4);
        set1.add(5);

        set2.add(15);
        set2.add(6);
        set2.add(6);

        System.out.println("Contents of set 'set1': ");
        set1.output();
        System.out.println("Contents of set 'set2': ");
        set2.output();
        System.out.println();
    }
}

【问题讨论】:

  • 我要感谢你们的快速回复,我还有一些工作要做,但是,我不再收到空指针异常,因为我创建了它的实例并分配了值.

标签: java arraylist set hashtable


【解决方案1】:

我不想直接给出答案,因为这可能是家庭作业(如果我错了,请纠正我)。考虑第一次在新构造的集合上调用 add 方法。此时“hashArray”的所有索引中的值是什么?这对于您的 add 方法中的局部变量“list”意味着什么?

【讨论】:

    【解决方案2】:

    这条线没有做你认为它正在做的事情。

    hashArray = new SLL[size];
    

    您实际上需要创建每个SLL,一旦创建了数组本身,就会填充数组。

    【讨论】:

    • 我将如何构造我的 output( ) 方法来引用 hashArray 和 SLL.outputList( )?
    • 你有一个选择,因为你不能修改你的构造函数(在你的代码中这么说),即在添加时创建节点本身(如果它为空)。当散列到一个空的“桶”时,如果你的对象应该存在的空间是null,你必须先使用new SLL()...创建它,然后再使用它。如果它不为空,那么您可以像在代码中一样使用它。您的输出方法可以使用简单的增强型 for 循环 (for (SLL listObj : hashArray) {...} ) 来遍历您的列表并打印其中的任何内容。请务必检查null...
    • 我昨晚在它完成前大约 20 分钟才发现这一点,我以前从未使用过这种类型的 for 循环语法,但我让它以这种方式工作。感谢所有的帮助。
    猜你喜欢
    • 2013-12-31
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多