0
public Solution getReferenceSolution(Problem p)
        throws UnsupportedOperationException {
        Solution result;

        if (!haveReferenceSolution)
            throw new UnsupportedOperationException("Domain.getReferenceSolution: A getReferenceSolution() method has not been specified for this domain.  If its use is required, please specify one using setEquivalenceClasses() or by overriding Domain.getReferenceSolution().");
        else {
            if (haveBooleanSolutionCutoff)
                result = findNearestEquivalenceClass(p).applyTo(p, booleanSolutionCutoff);
            else
                result = findNearestEquivalenceClass(p).applyTo(p);
        }

        result.setIsReferenceSolution(true);

        return result;
    }
2
  • Please explain more precisely what you expect to have in returned array. Commented Apr 11, 2011 at 6:54
  • I am having 2 types of classes. One class use this class to return non-array result. For another class, i want to use this class to return an array result. eg: Solution[] . How can I do it? Commented Apr 11, 2011 at 7:00

3 Answers 3

1

If you only need one solution normally, but one place needs multiple solutions, I suggest you have two methods; something like this:

public Solution getReferenceSolution(Problem p)
{
    // Code as before
}

public List<Solution> getAllSolutions(Problem p)
{
    // Whatever you need to do here
}

Note how it's now obvious from the method name whether you're looking for one solution or multiple ones; I wouldn't use overloading in this situation, as you're trying to do different things.

Sign up to request clarification or add additional context in comments.

Comments

0

Do you mean like this?

public Solution[] getReferenceSolution(Problem p) {
    Solution result;
    // set result.
    return new Solution[] { result };
}

3 Comments

this function is referred by other classes too. but those classes dont need array. How can I somehow override this function just for one class that want to return array?
how to declare it as an override?
The return types need to be the same for all implementations (or a sub class). Either you use an array to return on all of them or none of them. Another option is to have a Solution which contains an array of Solution. This would allow you to return an "array" inside a Solution.
0

Maybe better to return a collection, e.g. an ArrayList:

public List<Solution> getReferenceSolution(Problem p)
    throws UnsupportedOperationException {

    List<Solution> solutions= new ArrayList<Solution>();

    Solution result = ... // your code here

    solutions.add(result);

    return solutions;
}

Or maybe you want to pass the List as argument to the getReferenceSolution method and fill it inside the method?

public void getReferenceSolution(Problem p, List<Solution> solutions)
    throws UnsupportedOperationException {

    // your code to fill the list using solutions.add(Solution)

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.