• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

adding parameterized object in a Collection

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On my way studing AI, I've trying to develop an heuristic search and I come up with the following problem:

I have a fringe in a superclass:



for each different search strategies I have a subclass that instantiate a different kind of fringe, for example:



the add() has a compilation problem:

The method add(capture#2-of ? extends Node<T>) in the type Collection<capture#2-of ? extends Node<T>> is not applicable for the
arguments (Node<T>)

What's happening?
 
Saloon Keeper
Posts: 13248
291
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are trying to add a Node<T> to a collection that holds "something that is a subclass of Node<T>". What if your fringe happens to be an ArrayList<SubNode<T>>? Clearly, you are not allowed to add some Node<T> when it expects a SubNode<T>. One solution is to remove the upper bound on your fringe: simply make it a Collection<Node<T>>.
 
Mauricio Lopes
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stephan!

I've not mentioned that the code was as you told to be a Collection<Node<T>>. But on design progress I needed a subclass of Node<T> called HeuristicNode<T>. Somewhere else I've gonna need to add that subclass object to fringe. That's why the fringe must be as it is declared.

Can imagine any other solution!?
 
Stephan van Hulst
Saloon Keeper
Posts: 13248
291
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the neatest way is to probably just make it a Collection<Node<T>> and then simply add HeuristicNode<T> to it when you need to. Then when you retrieve nodes, simply check whether the node is a HeuristicNode:
 
And inside of my fortune cookie was this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic