Hey there!
I'm working on a project where I have the following classes and interfaces:
public interface I_SolutionCarrier
public class Agent
public class Actor extends Agent implements I_SolutionCarrier
public class ProblemRastrigin implements I_Problem
ProblemRastrigin contains a method that uses a Solution to calculate a value.
It can get this solution by calling the getSolution() method which is defined in I_SolutionCarrier and implemented by Actor.
Now I'd like to have a sort function in the class ProblemRastrigin that uses the getSolution() method to sort a LinkedList of actors. In the future however, different types of agents will carry a solution and thus the getSolution() method will be implemented in the Agent class.
Therefore I'd like the sort method in problemRastrigin to be Sort(LinkedList<I_SolutionCarrier>) instead of sort(LinkedList<Actor>). In principle this works fine, but for this to work I have to cast all elements of a LinkedList<Actor> to get a LinkedList<I_SolutionCarrier> before I can sort.
Someone told me it was possible to write a method sort(LinkedList<? extends I_SolutionCarrier> list)
But when I do this, I get errors in the swap method of the sort algorithm (quicksort)
private void swap(LinkedList<? extends I_SolutionCarrier> list, int i, int j) { I_SolutionCarrier tmp = list.get(i); list.set(i, list.get(j)); list.set(j, tmp); }
Both calls to list.set give the following erros:
Any ideas on how to get this working?The method set(int, capture#17-of ? extends I_SolutionCarrier) in the type LinkedList<capture#17-of ? extends I_SolutionCarrier> is not applicable for the arguments (int, capture#18-of ? extends I_SolutionCarrier)
Thanks in advance!