Re: Generics Problem



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
.



Relevant Pages

  • Re: Generics Problem
    ... Alex schreef: ... UnrootedNode, so I decided to make a class called Node and have both ... extends Node> internalNodes; ... What I don't understand is why allNodes.getis of type Node rather ...
    (comp.lang.java.help)
  • Re: Generics Problem
    ... I have two classes that are quite similar, RootedNode and ... UnrootedNode, so I decided to make a class called Node and have both ... extends Node> internalNodes; ... It would really help you to provide us with an SSCCE instead of these code fragments. ...
    (comp.lang.java.help)
  • Generics Problem
    ... I have two classes that are quite similar, RootedNode and ... UnrootedNode, so I decided to make a class called Node and have both ... extends Node> internalNodes; ... What I don't understand is why allNodes.getis of type Node rather ...
    (comp.lang.java.help)
  • Re: Generics Problem
    ... I have two classes that are quite similar, RootedNode and ... UnrootedNode, so I decided to make a class called Node and have both ... extends Node> internalNodes; ... What I don't understand is why allNodes.getis of type Node rather ...
    (comp.lang.java.help)
  • Re: Generics Problem
    ... I have two classes that are quite similar, RootedNode and ... UnrootedNode, so I decided to make a class called Node and have both ... RootedTree and Tree. ... extends Node> internalNodes; ...
    (comp.lang.java.help)