Java 如何使此函数返回多于1个值?
下面是类解决方案的原始代码:Java 如何使此函数返回多于1个值?,java,Java,下面是类解决方案的原始代码: public Solution getReferenceSolution(Problem p) { int personalityVal = 1; int templateNameVal = 0; ... Solution personality = getComponentFactory().constructSolution(personalityVal); Solution t
public Solution getReferenceSolution(Problem p) {
int personalityVal = 1;
int templateNameVal = 0;
...
Solution personality = getComponentFactory().constructSolution(personalityVal);
Solution templateName = getComponentFactory().constructSolution(templateNameVal);
...
return personality,templateName,..;
}
以下是内部域类代码:
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);
}
}
公共解决方案[]获取参考解决方案(问题p)
抛出不支持的操作异常{
求解结果;
如果(!haveReferenceSolution)
抛出新的UnsupportedOperationException(“Domain.getReferenceSolution:尚未为此域指定getReferenceSolution()方法。如果需要使用它,请使用SetEquivalenceClass()或重写Domain.getReferenceSolution()”;
否则{
if(haveBooleanSolutionCutoff)
结果=发现的线性等效类(p)。应用于(p,布尔解截止);
其他的
结果=发现的等效性等级(p)。适用于(p);
}
结果:setIsReferenceSolution(真);
返回结果;//使函数返回列表
或数组
或映射
或哈希表
或任何其他可容纳多个值的Java对象。使函数返回列表
或数组
或映射
或哈希表
或任何其他可容纳多个值的Java对象多个值。为什么不能返回一个解决方案数组?我认为这将是最简单的解决方案。为什么不能返回一个解决方案数组?我认为这将是最简单的解决方案。基本上-如果要返回多个值,您不需要创建一个只包含值的类并返回该类的实例,或者在案例-您可以返回解决方案数组,而不是仅返回一个:
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!
}
但即使在这里,您也只返回一个对象,不管它是数组、映射、列表还是包含几个值的某个类实例。基本上,如果您想返回多个值,您不需要创建一个只包含值的类并返回该类的一个实例,或者在您的情况下,您可以返回一个解决方案inst的数组只有一个:
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!
}
但即使在这里,您也只返回一个对象,不管它是数组、映射、列表还是包含一些值的类实例。您有很多选择
如果您只想返回int上的一个序列,那么simples将返回int[]
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;
}
或
如果您想返回键值类型的东西,请返回一个Map
,例如
return new Solution[]{personality, templateName};
你有很多选择
如果您只想返回int上的一个序列,那么simples将返回int[]
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;
}
或
如果您想返回键值类型的东西,请返回一个Map
,例如
return new Solution[]{personality, templateName};
已经提到了很多选项,但是您可能还需要考虑一个选项:创建一个新的结果类来封装您想要返回的所有内容。它可以清楚地显示返回的内容,而对于任意数组或集合,您不一定知道元素的用途。如果您需要,也可以这样做要返回更多的对象,这将更容易。已经提到了很多选项,但是您可能还需要考虑一个选项:创建一个新的结果类来封装您想要返回的所有对象。它可以清楚地显示返回的对象,而对于任意数组或集合,您不一定知道返回的对象是什么元素用于。如果您需要返回更多的对象,则更容易执行。为什么要返回personalityVal、templateNameVal而不是personality和templateName?ops..抱歉。错误代码。刚刚编辑:)您可以有另一种包含多个解决方案的解决方案吗?是的。我想这样做。但是我不知道如何使我当前的解决方案类支持数组类型的解决方案?为什么要返回personalityVal、templateNameVal而不是personality和templateName?ops..抱歉。错误的代码。刚刚编辑:)你能拥有另一种包含多个解决方案的解决方案类型吗?是的。我想这样做。但我不知道怎么做要使我当前的解决方案类支持解决方案的数组类型?我当前有用于解决方案的类。但它当前不是数组类型。我想知道如何将其扩展为数组类型?如何使非数组类成为数组类?我当前有用于解决方案的类。但它当前不是数组类型。我想知道如何将其扩展为数组类型现在,如何将其扩展为数组类型?如何使非数组类成为数组类?如果要将解决方案更改为解决方案[],应在解决方案类内部更改哪部分代码?我应该在我的原始解决方案类中放置什么?@karikari解决方案类不需要修改。您的getReferenceSolution需要修改以支持返回多个解决方案。调用此方法的代码将获得各种解决方案对象,如下所示:Solution[]sa=getReferenceSolution(…);Solution personality=sa[0];Solution template=sa[1];
将返回类型更改为Solution[]时,出现以下错误:返回类型与域不兼容。getReferenceSolution(问题)。如何解决此问题?您需要更改调用此方法的位置。您必须像Solution sol=domain.getReferenceSolution(..);
,将其更改为Solution[]sol=domain.getReferenceSolution(..);
然后获取返回的解决方案,如我前面的注释所述。如果我想将解决方案更改为解决方案[],我应该在解决方案类中更改代码的哪一部分?我应该在我的原始解决方案类中放置什么?@karikari解决方案类不需要修改。您的getReferenceSolution需要修改以支持返回多个解决方案。调用此方法的代码将获得各种解决方案对象,如下所示:Solution[]sa=getReferenceSolution(…);解决方案个性=sa[0];解决方案模板=sa[1];
当我更改退货类型时
//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
}