Solved

DownCast  or Upcast from type Node<E> to Object

Posted on 2007-03-17
12
506 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:Mayank S
ID: 18742552
A LinkedList class already exists in Java collections API - why do you want to recreate?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18742595
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
ID: 18742619
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 30

Expert Comment

by:Mayank S
ID: 18742632
No, the add () method is in the LinkedList. Node will just have a method for setting the data member.
0
 

Author Comment

by:Dirk6996
ID: 18742634
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 18742698
No, the LinkedList will be of a particular type and the node will be of that type automatically.
0
 

Author Comment

by:Dirk6996
ID: 18742736
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
ID: 18742741
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:
Mayank S earned 250 total points
ID: 18744499
>> 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
ID: 18953767
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

810 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