如何调用嵌套在我的 SLL class 中的节点 class?
How can I call the Node class nested within my SLL class?
现在我已经在我的 Java 程序中导入了一个单链接 class,它包含一个嵌套节点 class,如下所示:
public class SLList<T> extends AbstractQueue<T> {
class Node {
T x;
Node next;
}
//
//Some more code
//
}
我将我的 SLList class 实例化为通用
Queue<Integer> mySLL=new SLList<Integer>();
具体来说,现在,我希望能够获取两个节点(在我的例子中是整数)并仅切换这些节点的链接,以便它们在单向链表中切换位置。但是,当我尝试实例化它时,我无法访问该节点。到目前为止,我试过这个:
SLList.Node myNode= mySLL.new Node();
但是它说找不到Node()。有什么方法可以成功调用Node吗?
干杯
错误很严重,但问题是 Node
需要实例化 SLList
的封闭实例。
您为此使用的语法是正确的(除了使用原始类型):
Queue<Integer> mySLL = new SLList<Integer>();
SLList.Node myNode = mySLL.new Node();
但是,mySLL
在这里不是 SLList
,而是 Queue
。
要解决此问题,您可以将 mySLL
声明为 SLList
:
SLList<Integer> mySLL = new SLList<>();
SLList<Integer>.Node myNode = mySLL.new Node(); // now works
现在我已经在我的 Java 程序中导入了一个单链接 class,它包含一个嵌套节点 class,如下所示:
public class SLList<T> extends AbstractQueue<T> {
class Node {
T x;
Node next;
}
//
//Some more code
//
}
我将我的 SLList class 实例化为通用
Queue<Integer> mySLL=new SLList<Integer>();
具体来说,现在,我希望能够获取两个节点(在我的例子中是整数)并仅切换这些节点的链接,以便它们在单向链表中切换位置。但是,当我尝试实例化它时,我无法访问该节点。到目前为止,我试过这个:
SLList.Node myNode= mySLL.new Node();
但是它说找不到Node()。有什么方法可以成功调用Node吗? 干杯
错误很严重,但问题是 Node
需要实例化 SLList
的封闭实例。
您为此使用的语法是正确的(除了使用原始类型):
Queue<Integer> mySLL = new SLList<Integer>();
SLList.Node myNode = mySLL.new Node();
但是,mySLL
在这里不是 SLList
,而是 Queue
。
要解决此问题,您可以将 mySLL
声明为 SLList
:
SLList<Integer> mySLL = new SLList<>();
SLList<Integer>.Node myNode = mySLL.new Node(); // now works