Solved

DownCast  or Upcast from type Node<E> to Object

Posted on 2007-03-17
12
501 Views
Last Modified: 2013-11-23
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
Comment
Question by:Dirk6996
  • 5
  • 4
12 Comments
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
A LinkedList class already exists in Java collections API - why do you want to recreate?
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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
 

Author Comment

by:Dirk6996
Comment Utility
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
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
No, the add () method is in the LinkedList. Node will just have a method for setting the data member.
0
 

Author Comment

by:Dirk6996
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
No, the LinkedList will be of a particular type and the node will be of that type automatically.
0
 

Author Comment

by:Dirk6996
Comment Utility
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
 

Author Comment

by:Dirk6996
Comment Utility
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
 
LVL 30

Accepted Solution

by:
mayankeagle earned 250 total points
Comment Utility
>> 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
 
LVL 8

Assisted Solution

by:malfunction84
malfunction84 earned 250 total points
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now