Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java在将LinkedList实例化到数组时出现问题。数据结构课程_Java_Arrays_Doubly Linked List - Fatal编程技术网

Java在将LinkedList实例化到数组时出现问题。数据结构课程

Java在将LinkedList实例化到数组时出现问题。数据结构课程,java,arrays,doubly-linked-list,Java,Arrays,Doubly Linked List,我在网上搜遍了,找不到我问题的答案。我目前正在学习数据结构课程,这对这个问题很重要,因为我需要从头开始。我目前正在做家庭作业,问题是: 使用USet,实现一个袋子。一个包就像一个USet,它支持add(x)、remove(x)和find(x)方法,但它允许存储重复的元素。行李中的查找(x)操作返回某个等于x的元素(如果有)。此外,行李支持findAll(x)操作,该操作返回行李中等于x的所有元素的列表 我已经完成了几乎所有的工作,现在当我尝试测试我的代码时,它会立即抛出一个空指针异常。我通过了调

我在网上搜遍了,找不到我问题的答案。我目前正在学习数据结构课程,这对这个问题很重要,因为我需要从头开始。我目前正在做家庭作业,问题是:

使用USet,实现一个袋子。一个包就像一个USet,它支持add(x)、remove(x)和find(x)方法,但它允许存储重复的元素。行李中的查找(x)操作返回某个等于x的元素(如果有)。此外,行李支持findAll(x)操作,该操作返回行李中等于x的所有元素的列表

我已经完成了几乎所有的工作,现在当我尝试测试我的代码时,它会立即抛出一个空指针异常。我通过了调试器,虽然我知道它失败的地方(当我尝试创建数组列表并用链表填充它时),但我不知道如何修复它。以下是错误的状态:

Exception in thread "main" java.lang.NullPointerException 
at Bag.<init>(Bag.java:10)
at Bag.main(Bag.java:198)
线程“main”java.lang.NullPointerException中的异常 at Bag.(Bag.java:10) at Bag.main(Bag.java:198) 因为我甚至还没有启动它,我显然不知道它会遇到任何其他错误,但当这个问题解决后,我会面对这些错误。谢谢你的帮助

提醒:我不能使用预先构建的java字典,一切都需要从基础做起

这是我的全部代码:

public class Bag<T> {

        final int ARR_SIZE = 128;
        LinkedList[] theArray;

        public Bag() {

            for (int i = 0; i < ARR_SIZE; i++) {
                theArray[i] = new LinkedList();
            }
        }

        public boolean add(T x) {
            T element = x;
            int hashKey = element.hashCode() % ARR_SIZE;

            theArray[hashKey].addFirst(element, hashKey);
            return true;
        }

        public T find(T x) {

            T element = x;
            int hashKey = element.hashCode() % ARR_SIZE;

            return theArray[hashKey].findNode(element).getData();
        }

        public T findAll(T x) {
            T element = x;
            int hashKey = element.hashCode() % ARR_SIZE;

            System.out.print(theArray[hashKey].findAllElements(element));

            return element;
        }

        public T remove(T x) {
            T element = x;
            int hashKey = element.hashCode() % ARR_SIZE;
            return theArray[hashKey].removeElement(element);
        }

        public int size() {
            return ARR_SIZE;
        }

        public class Node {

            T data;
            int key;
            Node next;
            Node prev;

            public Node(T t, int k, Node p, Node n) {
                data = t;
                key = k;
                prev = p;
                next = n;
            }

            public T getData() {
                return data;
            }

            public void setData(T data) {
                this.data = data;
            }

            public Node getNext() {
                return next;
            }

            public void setNext(Node next) {
                this.next = next;
            }

            public Node getPrev() {
                return prev;
            }

            public void setPrev(Node prev) {
                this.prev = prev;
            }

            public void display() {
                System.out.println(data);
            }
        }

        public class LinkedList {

            Node header;
            Node trailer;
            int size = 0;

            public LinkedList() {
                header = new Node(null, -1, trailer, null);
                trailer = new Node(null, -1, null, null);
                header.setNext(trailer);
            }

            public int size() {
                return size;
            }

            public boolean isEmpty() {
                return size() == 0;
            }

            public void addFirst(T t, int hashKey) {
                Node currentLast = header.getNext();
                Node newest = new Node(t, hashKey, header, currentLast);
                header.setNext(newest);
                currentLast.setPrev(newest);
                size++;
            }

            public T add(T t) {
                Node currentLast = header.getNext();
                Node newest = new Node(t, -1, header, currentLast);
                header.setNext(newest);
                currentLast.setPrev(newest);
                size++;

                return newest.getData();
            }

            public T removeElement(T t) {
                if (isEmpty()) {
                    return null;
                }

                T element = t;
                return removeNode(findNode(element));
            }

            public T removeNode(Node node) {
                if (isEmpty()) {
                    return null;
                }

                Node pred = node.getPrev();
                Node succ = node.getNext();
                pred.setNext(succ);
                succ.setPrev(pred);
                size--;

                return node.getData();
            }

            public LinkedList findAllElements(T t) {
                Node current = header.getNext();
                T element = t;

                if (isEmpty()) {
                    return null;
                }

                LinkedList all = new LinkedList();

                while (current != null) {
                    if (current.getData() == element) {
                        all.addFirst(element, -1);
                    } else {
                        current = current.getNext();
                    }
                }
                return all;
            }

            public Node findNode(T t) {
                Node current = header.getNext();
                T element = t;

                if (isEmpty()) {
                    return null;
                }

                while (current.getNext() != null && current.getData() != element) {
                    current = current.getNext();
                }

                if (current.getNext() == null && current.getData() != element) {
                    System.out.println("Does not exist");
                }

                return current;
            }
        }

        public static void main(String[] args) {

            Bag<Integer> bag = new Bag();
            bag.add(1);
            bag.add(1);
            bag.add(2);
            bag.add(2);
            bag.add(8);
            bag.add(5);
            bag.add(90);
            bag.add(43);
            bag.add(43);
            bag.add(77);
            bag.add(100);
            bag.add(88);
            bag.add(555);
            bag.add(345);
            bag.add(555);
            bag.add(999);

            bag.find(1);
        }
    }
公共类包{
最终整数ARR_SIZE=128;
LinkedList[]theArray;
公文包(){
对于(int i=0;iLinkedList[] theArray = new LinkedList[ARR_SIZE];