【发布时间】:2015-02-05 14:01:39
【问题描述】:
我正在尝试在 java 中创建队列的链表实现,我们正在使用基本的 IQueue 接口,然后我们有一个列表元素类,它定义了列表/队列中的元素,然后我们还有一个 MyQueue 类的信息存储在。我将提供下面的所有代码。我还编写了一个 test.java 主类来运行它并查看它是否正常工作,然后当我编译并运行它时,我遇到了一些错误,这些错误也会显示出来。
界面:
/**
* An interface for a generic queue
*/
public interface IQueue<E> {
// Add an element to the (end of) queue.
public void enqueue(E element);
// Remove and return element from the (start of) queue.
public E dequeue();
// Returns true when the queue has no elements, false otherwise.
public boolean isEmpty();
}
列表元素:
public class ListElement<E> {
private final E value;
private ListElement<E> next;
private ListElement<E> prev;
public ListElement(E value) {
this.value = value;
}
public E getValue() {
return this.value;
}
public ListElement<E> getNext() {
return this.next;
}
public ListElement<E> getPrev() {
return this.prev;
}
public void setNext(ListElement<E> e) {
this.next = e;
}
public void setPrev(ListElement<E> e) {
this.prev = e;
}
}
MyQueue.java:
import java.util.ArrayList;
public class MyQueue<E> implements IQueue<E> {
private ListElement<E> head;
private ListElement<E> tail;
public MyQueue() {
head = null;
tail = null;
}
// INCOMPLETE.
public E modifyHead(E newhead) {
ListElement<E> update_head = new ListElement(newhead);
ListElement<E> oldhead = new ListElement(head);
head = update_head;
// Modifies the head of the queue to contain newhead.
// Returns the old value in the head.
return oldhead.getValue();
}
public boolean isEmpty() {
return (head == null);
}
public E dequeue() {
if (isEmpty()) {
return null;
}
ListElement<E> tmp = head;
head = tmp.getNext();
if (head == null) {
tail = null;
}
return tmp.getValue();
}
public void enqueue(E value) {
ListElement<E> tmp = new ListElement(value);
if (isEmpty()) {
tail = head = tmp;
} else {
tail.setNext(tmp);
tail = tmp;
}
}
public String toString() {
ArrayList a = new ArrayList();
while (head.getNext() != null){
a.add(head.getNext());
}
return a.toString();
}
}
Test.java:
public class test{
public static void main(String args[]){
MyQueue test = new MyQueue();
test.MyQueue();
test.enqueue("Kieran");
test.enqueue("Lavelle");
test.toString();
test.modifyHead("Replacement Head");
test.toString();
}
}
很抱歉有这么多代码,但我觉得这一切都有助于解决问题。在此先感谢,这是错误:
Exception in thread "main" java.lang.NoClassDefFoundError: test/java
Caused by: java.lang.ClassNotFoundException: test.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
有点意外,我跑错了。新错误:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2760)
at java.util.Arrays.copyOf(Arrays.java:2734)
at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
at java.util.ArrayList.add(ArrayList.java:351)
at MyQueue.toString(MyQueue.java:55)
at test.main(test.java:6)
【问题讨论】:
-
如何开始编写代码?看起来它在某些时候缺少权限或访问权限。
-
您做了什么导致该异常出现?即你是如何编译和运行这段代码的?
-
看起来你的类路径不正确。
-
你不需要运行
java test.java,如果你没有编译你的代码,使用java -cp . test(没有.java扩展名),使用javac test.java(和其他)编译它 -
公共类测试{ public static void main(String args[]){ MyQueue test = new MyQueue(); test.MyQueue();我不知道这里发生了什么,但看起来你正在调用 MyQueue 的构造函数?也应该避免使用没有大写的测试。
标签: java linked-list queue