如何在Java中创建包含在另一个类中的非静态类的对象?

如何在Java中创建包含在另一个类中的非静态类的对象?,java,linked-list,Java,Linked List,我正在尝试创建一个反转链接列表的方法。我有一个创建链表的类 public class LinkedList<t> { public class Node { t item; Node next; } private Node firstNode; public Node getFirstNode() { return this.firstNode; } public void appendToTail(

我正在尝试创建一个反转链接列表的方法。我有一个创建链表的类

public class LinkedList<t> {

    public class Node {
        t item;
        Node next;
    }


    private Node firstNode;

    public Node getFirstNode() { return this.firstNode; }

    public void appendToTail(t item){
        if(this.firstNode == null){
            this.firstNode = new Node();
            this.firstNode.item = item;
            return;
        }
        Node current = this.firstNode;
        while(current.next != null){
            current = current.next;
        }
        current.next = new Node();
        current.next.item = item;
    }
}
公共类链接列表{
公共类节点{
t项;
节点下一步;
}
私有节点firstNode;
公共节点getFirstNode(){返回this.firstNode;}
公共无效附件(t项){
if(this.firstNode==null){
this.firstNode=新节点();
this.firstNode.item=项目;
返回;
}
节点当前=this.firstNode;
while(current.next!=null){
当前=当前。下一步;
}
current.next=新节点();
current.next.item=项目;
}
}
反转链表的方法在我的“Main”类中

公共静态链接列表反向链接列表(链接列表l){
LinkedList.Node current=l.getFirstNode();
LinkedList reverse=新建LinkedList();
LinkedList.Node head=reverse.getFirstNode();
while(当前!=null){
LinkedList.Node newHead=newreverse.Node();
newHead.item=current.item;
newHead.next=head;
head=新head;
当前=当前。下一步;
}
反向返回;
}
对于我要添加到新反向链接列表前面的每个新节点,我需要创建一个“node”类的新实例,该类包含在“LinkedList”类中。“Node”类不能是静态的,因为其“item”属性设置为与“LinkedList”类型相同的泛型类型。因此,我需要一个类“LinkedList”的实例,以便访问“Node”类并创建其对象。在上面的代码中,我使用了“LinkedList”上的“reverse”实例来实现这一点。但是我得到一个错误,说“包反向不存在”。这一定是因为我试图将其作为一个包使用。我如何解决这个问题

我必须能够通过将“Node”类与“LinkedList”分离来解决这个问题。还有其他方法我可以不这样做吗?

更新此行:

LinkedList.Node newHead = new reverse.Node();
,待

LinkedList.Node newHead = reverse.new Node();
更新此行:

LinkedList.Node newHead = new reverse.Node();
,待

LinkedList.Node newHead = reverse.new Node();

如果要求仅反向链接列表,请使用Collections.reverse方法,该方法将列表作为参数并返回反向列表。

如果要求仅反向链接列表,使用Collections.reverse方法,该方法将列表作为参数并返回反向列表。

我认为将
节点创建为静态节点没有问题。这样做能解决问题吗?我已经更改了类型参数名称,以便更清楚地声明了什么以及何时重用现有类型参数

public class TestList<T> {

   private Node<T> head;

   private static class Node<X> {
      X element;
      Node<X> next;
   }

   public void add( T element ) {
      Node<T> node = new Node<T>();
      node.element = element;
      if( head != null )
         node.next = head;
      head = node;
   }

   public T get() {
      return head.element;
   }
}


class External {
   public static <Z> void reverse( TestList<Z> arg ) {
      TestList.Node<Z> temp = new TestList.Node<>();
      temp.element = arg.get();
      // etc.
   }
}
公共类测试列表{
专用节点头;
私有静态类节点{
X元素;
节点下一步;
}
公共无效添加(T元素){
节点=新节点();
node.element=元素;
if(head!=null)
node.next=头部;
头部=节点;
}
公共部门得不到{
返回头元素;
}
}
类外部{
公共静态无效反向(TestList arg){
TestList.Node temp=newtestlist.Node();
temp.element=arg.get();
//等等。
}
}

我认为将
节点创建为静态节点没有问题。这样做能解决问题吗?我已经更改了类型参数名称,以便更清楚地声明了什么以及何时重用现有类型参数

public class TestList<T> {

   private Node<T> head;

   private static class Node<X> {
      X element;
      Node<X> next;
   }

   public void add( T element ) {
      Node<T> node = new Node<T>();
      node.element = element;
      if( head != null )
         node.next = head;
      head = node;
   }

   public T get() {
      return head.element;
   }
}


class External {
   public static <Z> void reverse( TestList<Z> arg ) {
      TestList.Node<Z> temp = new TestList.Node<>();
      temp.element = arg.get();
      // etc.
   }
}
公共类测试列表{
专用节点头;
私有静态类节点{
X元素;
节点下一步;
}
公共无效添加(T元素){
节点=新节点();
node.element=元素;
if(head!=null)
node.next=头部;
头部=节点;
}
公共部门得不到{
返回头元素;
}
}
类外部{
公共静态无效反向(TestList arg){
TestList.Node temp=newtestlist.Node();
temp.element=arg.get();
//等等。
}
}

在这里不将
节点定义为静态可能是个坏主意。在这里不将
节点定义为静态可能是个坏主意。我不知道我能做到这一点。非常感谢。没有问题:)你能批准答案吗:)我不知道我能做到。非常感谢。没有问题:)你能批准答案吗:)