我正在使用单向链表来学习Java。单链表 - 无法对静态类型进行静态引用
我有一个工作链表,但它绑定到整数类型,我现在想通过改变所有的整数声明泛型类型E.
编译器不断抱怨说,它“实现一个通用的类型化链表无法对非静态类型E“进行静态引用。代码仍然运行。有谁知道如何解决这个错误“?
我认为这是与一般的E型是静态像整数或双或如果它是像字符串或其他类的引用类型。
public class TestingLinkedList<E> {
private E value;
private TestingLinkedList<E> next;
/*
* Default Constructor
*
* @param value an absolute E value for the current Node
*
* @param next an absolute RecursiveLinkedList value for the current Node
*/
public TestingLinkedList(E value, TestingLinkedList next) {
this.value = value;
this.next = next;
}
/*
* Constructor Empty, when user supplies an empty for the constructor uses
* value = - 1 and next = null as input parameters
*
* @param value an absolute int value for the current Node
* @param next an absolute RecursiveLinkedList value for the current Node
*/
public static final TestingLinkedList EMPTY = new TestingLinkedList(null,null)
{
public TestingLinkedList remove(E n) {
return this;
};
public String toString() {
return "";
};
};
/*
* if the current node is null return false
* else if current value is the chosen value
* then return true. Otherwise call the contains
* method of the next item in queue
*
* @param value an absolute int value for the current Node
* @param RecursiveLinkedList object of the remove item
* */
public TestingLinkedList remove(E n) {
if (value == n) {
return next;
}
// Call the remove method of the next Node if the selected Node is not
// the current node then construct and return the next method
return new TestingLinkedList(value, next.remove(n));
}
/*
* if the current node is null return false
* else if current value is the chosen value
* then return true. Otherwise call the contains
* method of the next item in queue
*
* @param value an absolute int value for the current Node
* @param boolean
* */
public boolean contains(E n) {
if (next == null) {
return false;
}else if (value == n) {
return true;
} else {
return next.contains(n);
}
}
public String toString() {
return value + "," + next.toString();
}
/*
*
* Testing Methods for RecursiveLinkedList
*
* */
public static void main(String[] args) {
TestingLinkedList l = new TestingLinkedList(1,
new TestingLinkedList(2, new TestingLinkedList(2,
new TestingLinkedList(3, new TestingLinkedList(4,
EMPTY)))));
System.out.println(" Test to String Method : " + l.toString());
System.out.println(" Test remove method " + l.remove(1).toString());
System.out.println(" Test contains method "
+ String.valueOf(l.contains(4)));
}
}
您的错误在哪里? – Zyerah 2013-04-07 03:13:53
块if'(value == n){...}'也是可疑的。这应该是一个Object.equals()调用。 – 2013-04-07 03:58:35