1
public Solution getReferenceSolution(Problem p) {

        int personalityVal = 1;
        int templateNameVal = 0;
        ...

        Solution personality = getComponentFactory().constructSolution(personalityVal);
        Solution templateName = getComponentFactory().constructSolution(templateNameVal);
        ...

        return personality,templateName,..;
    }

Here is the original code for class Solution:

public class Solution
extends Object
implements java.io.Serializable,
edu.indiana.iucbrf.feature.Featured,
Comparable,
edu.indiana.util.xml.XMLRepresentable {
    private FeatureKey firstFeatureKey;
    private FeatureCollection features;

    /** Creates new Solution.
     */
    protected Solution() {
    }

    public Solution(FeatureCollection features,
    Domain domain) {

        this.features = features;
    }

    public boolean getIsReferenceSolution() {
        return features.getIsReferenceSolution();
    }

    public void setIsReferenceSolution(boolean isReferenceSolution) {
        features.setIsReferenceSolution(isReferenceSolution);
    }


    public boolean equals(Object other) {
        return (this.compareTo(other) == 0);
    }
}

Here is inside Domain class code:

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;  //<---- error over here!
    }
4
  • why would you want to return personalityVal, templateNameVal instead of personality and templateName? Commented Apr 11, 2011 at 5:53
  • ops..sorry. wrong code. just edited :) Commented Apr 11, 2011 at 6:08
  • Can you have another type of Solution which contains a number of Solutions? Commented Apr 11, 2011 at 6:14
  • Yep. I would like to do that. But I dont know how to make my current Solution class to support array type of Solution? Commented Apr 11, 2011 at 6:18

5 Answers 5

4

You have plenty of option.

  1. IF you just want to return a series on int, simplets is to return int[]. e.g.

    return new int[]{personalityVal, templateVal};
    

    or

    return new Solution[]{personality, templateName};
    
  2. If you want to return key-value kind of thing return a Map e.g.

       Map<String, Integer> m = new HashMap<String, Integer>();
       m.put("personalityVal", personalityVal);
       m.put("templateVal", templateVal);
       return m;
    

    or

       Map<String, Solution> m = new HashMap<String, Solution>();
       m.put("personality", personality);
       m.put("templateName", templateName);
       return m;
    
  3. You may return any other Collection. E.g. Set if you want to return unique values, List which is pretty much same as Array but more extensive.

//declare return type as Solution array
public Solution[] getReferenceSolution(Problem p) {

    int personalityVal = 1;
    int templateNameVal = 0;
    ...

    Solution personality = getComponentFactory().constructSolution(personalityVal);
    Solution templateName = getComponentFactory().constructSolution(templateNameVal);
    ...

    return new Solution[]{personality, templateName}; //return the array
}
Sign up to request clarification or add additional context in comments.

5 Comments

what part of code should I change inside Solution class if I want to change Solution to Solution[]?
what should i put inside my original Solution class?
@karikari there is no modification required to Solution class. Your getReferenceSolution is needed to be altered to support returning multiple solutions. The code that is calling this method would get various Solution objects something like this: Solution[] sa = getReferenceSolution (...); Solution personality = sa[0]; Solution template = sa[1];
When I change the return type to Solution[], I got this error: The return type is incompatible with Domain.getReferenceSolution(Problem). How to fix this?
You need to change the places where you are calling this method. You must be calling like Solution sol = domain.getReferenceSolution(..);, change it to Solution[] sol = domain.getReferenceSolution(..); and then get the returned solutions as mentioned in my prev comment.
3

Basically - you don't, if you want to return more than one value, create a class that only holds values and return an instance of this class, or in your case - you can return an array of solution instead of just one:

public Solution[] getReferenceSolution(Problem p) {

        int personalityVal = 1;
        int templateNameVal = 0;
        ...
        Solution[] res = new Solution[2];
        res[0] = getComponentFactory().constructSolution(personalityVal);
        res[1] = getComponentFactory().constructSolution(templateNameVal);
        ...

        return res;
    }

But even here- you return only one object whether it's an array, map, list or some class instance that holds a few values.

Comments

1

There are plenty of options already mentioned, however there is one more you might want to consider: create a new result class to encapsulate everything you want to return. It makes it clear what's being returned, whereas with an arbitrary array or collection you don't necessarily know what the elements are intended for. As well if you ever need to return more objects it'll be easier to do.

Comments

0

Make the function to return List or array or Map or HashTable or any other Java object that can hold multiple values.

Comments

0

Why can't you return a Solution Array? I think it will be the easiest solution.

1 Comment

I currently have the class for Solution. but it is currently not an array type. I want to know how to extends it to become array type? How to make a non-array class to become an array class?

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.