修复导入CtCILibrary导致的错误

时间:2015-10-24 09:57:34

标签: java

我想了解Cracking The Coding Interview本书中的一个链接列表答案。我已经复制并粘贴了她的GitHub中的一个答案,当我在Question.java:5: error: package CtCILibrary does not exist import CtCILibrary.LinkedListNode;中尝试import CtCILibrary.LinkedListNode;时,我收到此错误:Question.java。所以起初,我认为这就像包含这样的Java库:import java.util.Hashtable;。但我收到错误并意识到import CtCILibrary.LinkedListNodejava.util.Hashtable不同。所以我决定上网,然后将http://ctci.googlecode.com/svn-history/r22/trunk/Java/CtCILibrary/CtCILibrary/LinkedListNode.java中的代码复制并粘贴到我的一个文本编辑器上,并将其命名为LinkedListNode.java。现在,我收到了这个错误:

Question.java:7: error: cannot access LinkedListNode
    public static void deleteDupsA(LinkedListNode n) {
                                   ^
  bad source file: ./CtCILibrary.LinkedListNode.java
    file does not contain class LinkedListNode
    Please remove or make sure it appears in the correct subdirectory of the sourcepath.

我被困在如何离开这里。问题是,LinkedListNode.java与我的Question.java位于同一目录中。因此,我不确定能够使用LinkedListNode.java需要做些什么。有关如何解决此错误的任何建议:

这是我的Question.java

import java.util.HashSet;
import java.util.Hashtable;

import CtCILibrary.LinkedListNode;

public class Question {
    public static void deleteDupsA(LinkedListNode n) {
        HashSet<Integer> set = new HashSet<Integer>();
        LinkedListNode previous = null;
        while (n != null) {
            if (set.contains(n.data)) {
                previous.next = n.next;
            } else {
                set.add(n.data);
                previous = n;
            }
            n = n.next;
        }
    }

    public static void deleteDupsC(LinkedListNode head) {
        if (head == null) return;
        LinkedListNode previous = head;
        LinkedListNode current = previous.next;
        while (current != null) {
            // Look backwards for dups, and remove any that you see.
            LinkedListNode runner = head;
            while (runner != current) { 
                if (runner.data == current.data) {
                    LinkedListNode tmp = current.next;
                    previous.next = tmp;
                    current = tmp;
                    /* We know we can't have more than one dup preceding
                     * our element since it would have been removed 
                     * earlier. */
                    break;
                }
                runner = runner.next;
            }

            /* If runner == current, then we didn't find any duplicate 
             * elements in the previous for loop.  We then need to 
             * increment current.  
             * If runner != current, then we must have hit the ‘break’ 
             * condition, in which case we found a dup and current has
             * already been incremented.*/
            if (runner == current) {
                previous = current;
                current = current.next;
            }
        }
    }

    public static void deleteDupsB(LinkedListNode head) {
        if (head == null) return;

        LinkedListNode current = head;
        while (current != null) {
            /* Remove all future nodes that have the same value */
            LinkedListNode runner = current;
            while (runner.next != null) { 
                if (runner.next.data == current.data) {
                    runner.next = runner.next.next;
                } else {
                    runner = runner.next;
                }
            }
            current = current.next;
        }
    }   

    public static void main(String[] args) {    
        LinkedListNode first = new LinkedListNode(0, null, null); //AssortedMethods.randomLinkedList(1000, 0, 2);
        LinkedListNode head = first;
        LinkedListNode second = first;
        for (int i = 1; i < 8; i++) {
            second = new LinkedListNode(i % 2, null, null);
            first.setNext(second);
            second.setPrevious(first);
            first = second;
        }
        System.out.println(head.printForward());
        LinkedListNode clone = head.clone();
        deleteDupsA(head);
        System.out.println(head.printForward());
        deleteDupsC(clone);
    }
}

这是我的LinkedListNode.java

package CtCILibrary;

public class LinkedListNode {
    public LinkedListNode next;
    public LinkedListNode prev;
    public LinkedListNode last;
    public int data;
    public LinkedListNode(int d, LinkedListNode n, LinkedListNode p) {
        data = d;
        setNext(n);
        setPrevious(p);
    }

    public void setNext(LinkedListNode n) {
        next = n;
        if (this == last) {
            last = n;
        }
        if (n != null && n.prev != this) {
            n.setPrevious(this);
        }
    }

    public void setPrevious(LinkedListNode p) {
        prev = p;
        if (p != null && p.next != this) {
            p.setNext(this);
        }
    }   

    public String printForward() {
        if (next != null) {
            return data + "->" + next.printForward();
        } else {
            return ((Integer) data).toString();
        }
    }

    public LinkedListNode clone() {
        LinkedListNode next2 = null;
        if (next != null) {
            next2 = next.clone();
        }
        LinkedListNode head2 = new LinkedListNode(data, next2, null);
        return head2;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以在同一个java文件中包含LinkedListNode类并尝试访问它,它将运行。无需将其保存在单独的包和单独的文件中。