[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 524
  • Last Modified:

DownCast or Upcast from type Node<E> to Object

In java I am trying to implement a new LinkList  class that implements the iterator interface so only using iterator. The problem is I want to cast whatever type the instance of the Node is like say of type <Integer>. In  my class the type that needs to be cast is not determined until the tester creates an LinkedList of a certain type with that certian Node type. I need some sort of downcast from type Object to whatever type is neded I have an instance field that is type <E> just need some downcast help.

//instance field
private Node <E> Item;
//initialazation of instance field in constructor
//Something like this I think
Item = (E) new Object<Node>;//I don't know how? please help thanks

0
Dirk6996
Asked:
Dirk6996
  • 5
  • 4
2 Solutions
 
Mayank SAssociate Director - Product EngineeringCommented:
A LinkedList class already exists in Java collections API - why do you want to recreate?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
BTW Node should probably be instantiated as:

public void add ( E data )
{
  Node node = new Node () ;
  node.setData ( data ) ;

}

assuming you defined LinkedList as public class LinkedList<E> and the Node class as;

public class Node<E>
{
  private E data ;
  public void setData ( E data )
  {
    this.data = data ;
  }
  public E getData ()
  {
    return data ;
  }
}
0
 
Dirk6996Author Commented:
Are you saying in order to create new Nodes of type <E> I need to write an add method in my innerclass Node in order to create new nodes and to get nodes with a get method.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Mayank SAssociate Director - Product EngineeringCommented:
No, the add () method is in the LinkedList. Node will just have a method for setting the data member.
0
 
Dirk6996Author Commented:
Node node = new Node () ;
  node.setData ( data ) ;

Dont Nodes need to be of a particular type or cast down?
Can you tell me what this means or whats wrong with it
 node = new Node<E>(Object);
0
 
Mayank SAssociate Director - Product EngineeringCommented:
No, the LinkedList will be of a particular type and the node will be of that type automatically.
0
 
Dirk6996Author Commented:
So, does casting need to happen for the constructor in my Linked List class
For the instance field private Node <E> head = null;
the initilazation needs a cast to make sure that when a new Node is created that it is of the same type as the LinkList right?

Or also or just for the innerclass private class ListIter implements ListIterator <E>
I am implementing LinkList with most of the delegation being done in ListIter don't I need some casting in the constructor here.
0
 
Dirk6996Author Commented:
there's definetly a type issue somewhere because all the instance fields created throughout the entire program cannot be resolved to a type. hmmmm
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> private Node <E> head = null;

You should remove the <E> dependency from Node I guess. It should just be like private Node head = null ; Let the <E> stuff be at the LinkedList level.
0
 
malfunction84Commented:
When you declare the type of the IterableLinkedList, you bind a type to the type parameter E wherever it is used.  So you definitely want to define it as "class IterableLinkedList<E> implements ListIterator<E>" with a "private Node<E> head" field.  By default, head will be null.

If you're declaring Node as a generic private inner class with a type parameter representing the data it is holding (which is going to be the same type as the linked list), it actually MUST use a different identifier for the type parameter than what your linked list class is using.  If you use the same identifier, one will hide the other (much like variables or method parameters can hide a class's fields if the names match).

public class IterableLinkedList<E> implements ListIterator<E> {
    private Node<E> head;

    // Declaring the class as "Node<E>" here will hide the type E from IterableLinkedList
    private class Node<F> {
        private F data;
        private Node<F> next;
        ...
    }
    ...
}

However, if they're using different identifiers, how do we know that the Nodes will be bound to the same type that the IterableLinked is bound to?  The part which ensures that is the declaration of the head field, where we say Node<E>.  This way, when you bind a type to E in the IterableLinkedList, the same type is also bound to that head Node<E>.  All methods you write for IterableLinkedList will use the types E and Node<E>, and all the methods you write for Node will use F and Node<F>.  (If Node were defined outside of the IterableLinkedList class, we could use E in both places, but it would still be the same concept... just without the added confusion of hidden types.)

Your class would end up looking like this:

public class LinkedListIterator<E> implements ListIterator<E> {
    private Node<E> head = null;
   
    private class Node<F> {
        private F data;
        private Node<F> next;
       
        public Node() {
            this(null);
        }
       
        public Node(F data) {
            this(data, null);
        }
       
        public Node(F data, Node<F> next) {
            setData(data);
            setNext(next);
        }
       
        public F getData() {
            return data;
        }
       
        public void setData(F newData) {
            this.data = newData;
        }
       
        public Node<F> getNext() {
            return next;
        }
       
        public void setNext(Node<F> newNext) {
            this.next = newNext;
        }
    }

    // implementations of methods from List and Iterator interfaces...

    // for example, this adds to the head of the list
    public void add(E element) {
        // head = new Node<E>(element, head);
        Node<E> toBeAdded = new Node<E>(element);
        toBeAdded.setNext(head);
        head = toBeAdded;
    }

    // and so on...
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now