【问题标题】:Singly linked list - Cannot make a static reference to the non-static type单链表 - 无法对非静态类型进行静态引用
【发布时间】:2013-04-07 03:11:49
【问题描述】:

我正在使用单链表来学习 Java。

我有一个工作链表,但它绑定到整数类型,现在我试图通过将所有整数声明更改为泛型 E 来实现一个泛型类型链表。

编译器一直抱怨它“无法对非静态类型 E 进行静态引用”。代码仍然运行。有谁知道如何解决这个错误“?

我认为这与泛型类型 E 是像 integer 还是 double 这样的 static 还是像 String 或其他类这样的引用类型有关。

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)));
}

}

【问题讨论】:

  • 区块if (value == n) {...} 也很可疑。那应该是一个Object.equals() 电话。

标签: java linked-list generic-list type-erasure


【解决方案1】:
public static final TestingLinkedList EMPTY = new TestingLinkedList(null,null)

应该是:

public static final TestingLinkedList<Integer> EMPTY
                                      = new TestingLinkedList<Integer>(null,null)

这违反了泛型,因为E 的占位符是在构造时确定的,而且这是静态的,它不能使用E 值。而是使用通用方法:

public static <T> TestingLinkedList<T> empty() {
    return new TestingLinkedList<T>(null, null) {

        public TestingLinkedList remove(T n) {
            return this;
        }

        public String toString() {
            return "";
        }
    }
} 

【讨论】:

  • 非常感谢您的帮助,但我对您实现“空”的方式有疑问。我可能是错的,但编译器仍在抱怨。
  • 哦,对不起。这应该会有所帮助,请参阅编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-15
  • 2015-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-25
相关资源
最近更新 更多