如何重构:外部类SLList<;T>;具有内部类赋值<;T>;。内部类是否可以移动到自己的.java文件中?
我正在为一个作业编写我自己的单链表类。我最初在根目录中有自己的java文件中的Node类,但它将generic如何重构:外部类SLList<;T>;具有内部类赋值<;T>;。内部类是否可以移动到自己的.java文件中?,java,list,Java,List,我正在为一个作业编写我自己的单链表类。我最初在根目录中有自己的java文件中的Node类,但它将genericT分配给obj,因此看起来它必须嵌套在SLList中 我的问题(与赋值无关):有没有办法从外部类中提取节点类,或者必须在外部类中定义它,因为它继承了赋值 public class SLList<T> { Node head; // Front of the queue Node tail; // Back of the queue int count
T
分配给obj,因此看起来它必须嵌套在SLList
中
我的问题(与赋值无关):有没有办法从外部类中提取节点
类,或者必须在外部类中定义它,因为它继承了赋值
public class SLList<T> {
Node head; // Front of the queue
Node tail; // Back of the queue
int count; // The number of elements in the queue
public class Node {
T obj; // The stored object in this node.
Node next; // The reference to the next object, null if final element.
}
...
...
...
可以,但是需要使用通用参数定义
节点
。也就是说,类节点{T obj;Node next;}
。别忘了为声明的head
和tail
类型提供它们的类型参数,即Node
head`和Node tail
。好吧,但我有一个问题:请参见编辑。变量还需要知道它们的类型参数:Node head代码>
...
...
public T remove() {
// If empty, just return null:
if (count == 0)
return null;
// Set
T obj = head.obj; // ERROR: Object cannot be converted to type T.
head = head.next;
if (--count == 0)
tail = null;
return obj;
}