samman adhikari
samman adhikari

Reputation: 651

How to create object of a non static class enclosed inside another class in Java?

I am trying to create a method that reverses a Linked List. I have a class that creates a 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(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;
    }
}

The method for reversing the linked list is in my "Main" class

public static LinkedList reverseLinkedList(LinkedList<Integer> l){
        LinkedList.Node current = l.getFirstNode();
        LinkedList<Integer> reverse = new LinkedList<Integer>();
        LinkedList.Node head = reverse.getFirstNode();
        while(current != null){
            LinkedList.Node newHead = new reverse.Node();
            newHead.item = current.item;
            newHead.next = head;
            head = newHead;
            current = current.next;
        }
        return reverse;
    }

For every new node that I want to add in the front of my new reversed linked list I need to create a new instance of "Node" class which enclosed inside the "LinkedList" class. "Node" class cannot be static since its "item" attribute is set to the same generic type from "LinkedList" type. So, I need a instance of the class "LinkedList" so that I can access "Node" class and create its object. In the above code I have used the "reverse" instance on "LinkedList" to do exactly that. But I get an error saying "Package reverse does not exist". This must be because I am trying to use it as a package. How may I solve this issue?

I must be able to solve this issue by separating "Node" class from "LinkedList". Is there anyway else I can do so without doing that?

Upvotes: 0

Views: 92

Answers (3)

markspace
markspace

Reputation: 11030

I'm not seeing a problem with creating Node as static. Does doing it this way fix the problem? I've changed the type parameter names to make it more clear what is being declared and when an existing type parameter is being reused.

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.
   }
}

Upvotes: 0

Rajkumar
Rajkumar

Reputation: 134

If the requirement is only to reverse linkedlist, use Collections.reverse method which takes a list as a parameter and returns the reversed list.

Upvotes: 0

0xh3xa
0xh3xa

Reputation: 4857

Update this line:

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

, To be

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

Upvotes: 2

Related Questions