Re: Generics Problem
- From: Lew <lew@xxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 27 Mar 2007 09:27:59 -0400
Alex wrote:
Hi,
I am trying to use generics in my code and am having a problem.
I have two classes that are quite similar, RootedNode and
UnrootedNode, so I decided to make a class called Node and have both
of these extend it. I have a similar structure with UnrootedTree,
RootedTree and Tree. In Tree I define:
protected List<? extends Node> allNodes;
protected List<? extends Node> internalNodes;
protected List<? extends Node> externalNodes;
Then in UnrootedTree I have this code:
internalNodes = new ArrayList<UnrootedNode>();
externalNodes = new ArrayList<UnrootedNode>();
allNodes = new ArrayList<UnrootedNode>();
NewickParser np = new NewickParser(t);
allNodes = np.getNodes(); //this method returns List<UnrootedNode>
for(int i=0;i<allNodes.size();i++)
{
if(allNodes.get(i).isExternal())
externalNodes.add(allNodes.get(i)); // error
else
internalNodes.add(allNodes.get(i)); //error
}
The problem is both the lines marked error say:
E:\webb\Personal\Java\NetBeans\DataRead\src\webb\tree
\UnrootedTree.java:38: cannot find symbol
symbol : method add(webb.tree.Node)
location: interface java.util.List<capture of ? extends
webb.tree.Node>
externalNodes.add(allNodes.get(i));
What I don't understand is why allNodes.get(i) is of type Node rather
than type UnrootedNode and thus won't let me add it. Any help would be
apreciated...
The lists are declared List<? extends Node> - the wildcard deprives the List of the information it needs to safely insert values to the List, since it cannot know which subtype of Node is in the List.
-- Lew
.
- References:
- Generics Problem
- From: Alex
- Generics Problem
- Prev by Date: Re: Generics Problem
- Next by Date: Re: Generate Website(HTML) thumbnails on the fly
- Previous by thread: Re: Generics Problem
- Next by thread: Re: Generics Problem
- Index(es):
Relevant Pages
|
|