Results 1 to 5 of 5
- 12-20-2010, 08:15 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Casting failed when iterating over a Set
I'm working on a targeting system and having a problem when I iterate over a Set of Bogeys.
Here's the code:
public class BogeySet<Bogey> extends TreeSet<Bogey> {
public Bogey get(String name) {
for (Bogey bogey : this) {
if (bogey.getName().equals(name)) {
return bogey;
break;
}
}
return null;
}
}
And here's the error:
BogeySet.java:7: cannot find symbol
symbol : method getName()
location: class java.lang.Object
if (bogey.getName().equals(name)) {
1 error
It seems to me that I am clearly specifying that the Objects within the set are Bogeys, so why is Java considering them merely Objects and complaining?
By the way, there is a reason why I'm using a Set instead of a Map. For most purposes within my program I want to consider this a NavigableSet, and it seemed easier to simply add a getter to my Set than to adapt a Map to sort by value and get the values throughout the rest of my code.
- 12-20-2010, 08:19 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
are you meaning
class BogeySet<E extends Bogey> :confused:
and return bogey; break; makes no sense. Delete the break; !
- 12-20-2010, 08:54 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
E extends Bogey?
BogeySet extends TreeSet, and BogeySets only contain Bogeys.
As for "<E extends Bogey>", I don't know what you mean. That makes no sense to me or to my compiler.
You are correct, of course, that my break command would never execute. So I removed it.
But what's causing this "cannot find symbol" error? Bogey has a getName() method and it would be executed if Java considered bogey to be a Bogey. But Java is considering bogey to be simply an Object, which has no getName() method. Very strange.
- 12-20-2010, 09:00 PM #4
If I'm not mistaken that should be
dbJava Code:public class BogeySet extends TreeSet<Bogey> {
- 12-20-2010, 11:14 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Iterating over a map of objects
By javaaz in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 07-08-2010, 10:29 PM -
Iterating through ArrayList using a GUI
By DC200 in forum New To JavaReplies: 0Last Post: 08-02-2009, 01:24 AM -
Iterating through a HashSet
By Java Tip in forum Java TipReplies: 0Last Post: 01-21-2008, 04:34 PM -
Iterating through ArrayList
By Java Tip in forum Java TipReplies: 0Last Post: 01-20-2008, 08:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks