Results 1 to 11 of 11
- 06-25-2011, 09:22 PM #1
Type-Casting Object doesn't Give me the Methods I need
Hello Friends
I am slightly stuck and I would greatly appreciate any help.
I have a Node class, and this class has several methods, one of which is [ getName() ] which returns a String that represents the name of the node.
Here is my code:
A couple of things are happening here (I hope you can see), but the gist of it is that I am iterating through a Hashtable.Java Code:if(isAtEntrance()) { // A place to store the safe paths. ArrayList<Stack<String>> paths = new ArrayList<Stack<String>>(); // The variable g stands for, and is the Graph object < which in itself is a Hashtable. Hashtable<Node<Adjustment>, ArrayList<Node<Adjustment>>> g = getMap().getAdjacencyList(); // Iterating through [g] to find safe paths. Enumeration e = g.keys(); while(e.hasMoreElements()) { // A variable called [neighbours]. It represents the neighoubrs of the current node in the loop ArrayList<Node<Adjustment>> neighbours = g.get((Node<Adjustment>)e.nextElement()); // The Path [p] represented as a Stack. This will be inserted into our safe [paths] ArrayList later on. Stack<String> p = new Stack<String>(); // Iterating through the neighbours of the current node, and foreach building a path [p] from them. for(Node n : neighbours) { p.push((Node<Adjustment>)e.nextElement().getName()); p.push(n.getName()); } } }
e.nextElement() is an object. I am type casting it to a Node<Adjustment> but it isn't working. How do I know it isn't working ? .. It isn't giving me my getName() method.
What am I doing wrong ? I would greatly appreciate any help.
Thank You.>> What can be asserted without proof can be dismissed without proof. <<
- 06-25-2011, 09:25 PM #2
Member
- Join Date
- Jun 2011
- Posts
- 23
- Rep Power
- 0
did you try consulting the API ?
- 06-25-2011, 11:03 PM #3
The first thing I see wrong with that code is multiple calls to e.nextElement() in a while(e.hasMoreElements()) loop. That's almost guaranteed to blow up.
db
- 06-25-2011, 11:14 PM #4
Not really. I'm not sure what I'd be checking in the API.
I did google Type Casting in Java, cause I believe that's where the issue lies. am I wrong ?
>> What can be asserted without proof can be dismissed without proof. <<
- 06-25-2011, 11:37 PM #5
Try debugging your code by assigning the value returned by nextElement to a variable and printing out that variable to see what is being returned. That will require adding a few more statements.
- 06-26-2011, 12:06 AM #6
Thank You DarrylBruke
I did not realise that I had to only have it once within the loop. I have changed my code to the following:
Seems to be OK now ! Eclipse does NOT give any errors !Java Code:if(isAtEntrance()) { // A place to store the safe paths. ArrayList<Stack<String>> paths = new ArrayList<Stack<String>>(); // The variable g stands for, and is the Graph object < which in itself is a Hashtable. Hashtable<Node<Adjustment>, ArrayList<Node<Adjustment>>> g = getMap().getAdjacencyList(); // Iterating through [g] to find safe paths. Enumeration e = g.keys(); while(e.hasMoreElements()) { // e.nextElement can only be used once within the while loop. So I'm assigning it to a variable that can be used many times. Node<Adjustment> x = (Node<Adjustment>)e.nextElement(); // A variable called [neighbours]. It represents the neighoubrs of the current node in the loop ArrayList<Node<Adjustment>> neighbours = g.get(x); // The Path [p] represented as a Stack. This will be inserted into our safe [paths] ArrayList later on. Stack<String> p = new Stack<String>(); // Iterating through the neighbours of the current node, and foreach building a path [p] from them. for(Node n : neighbours) { p.push(x.getName()); p.push(n.getName()); } } }
Can some Eclipse master please tell me what I'm suppose to do when I get a warning such as:
orNode is a raw type. References to generic type Node<NodeInfo> should be parameterized
Do I ignore these cause they are Warnings and not Errors ? Or are they serious and I need to do something about them ? .. if the latter, I have no idea what to do !Enumeration is a raw type. References to generic type Enumeration<E> should be parameterized
>> What can be asserted without proof can be dismissed without proof. <<
- 06-26-2011, 12:09 AM #7
You need to use generics with those classes the same as you have used it in other places in your code.
For example what type does g.keys() return?
- 06-26-2011, 12:21 AM #8
Hello Norm
Still slightly confused ! g.keys() returns Enumeration<Node<Adjustment>>
Or at least that's what Eclipse says when I hover over the word keys(). !!
Sincere apologies if my dumbness in Java is causing you guys stress and unhappy feelings.
>> What can be asserted without proof can be dismissed without proof. <<
- 06-26-2011, 12:29 AM #9
Look at what is to the left of "= g.keys();" and then look at the error message.g.keys() returns Enumeration<Node<Adjustment>>
You forgot to post the lines that go with the error messages.
How did you code this code using so many generics and then have this problem???Last edited by Norm; 06-26-2011 at 12:32 AM.
- 06-26-2011, 12:46 AM #10
Here is the code:
And here's its error:Java Code:Enumeration e = g.keys();
I think I got what you mean now though. It turns out that the best practice is to make lucid the type of all variables. So I went ahead and did this:Enumeration is a raw type. References to generic type Enumeration<E> should be parameterized
And violla ! the warning is gone !Java Code:Enumeration<Node<Adjustment>> e = g.keys();
Thank You Ever So Much.
Last edited by Ciwan; 06-26-2011 at 12:48 AM.
>> What can be asserted without proof can be dismissed without proof. <<
- 06-26-2011, 01:07 AM #11
Similar Threads
-
Type casting
By kiros88 in forum New To JavaReplies: 17Last Post: 09-02-2010, 01:58 PM -
Type Casting
By Shaheen Mohamed in forum New To JavaReplies: 6Last Post: 08-17-2010, 07:56 PM -
Casting Object to another type
By green_river48 in forum New To JavaReplies: 12Last Post: 04-03-2010, 10:52 AM -
help with type casting.
By ramsrocker in forum Java AppletsReplies: 15Last Post: 02-26-2009, 11:28 PM -
type casting
By alvations in forum New To JavaReplies: 1Last Post: 10-13-2008, 07:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks