2016-03-23 34 views
-1

我写我的代码,我想在返回多个值:如何在java中返回多个值?

public int[] getResult(){ 
return geneticAlgorithm(cost,profit,gens,turns,cmax); 

,但是当我用这个告诉我eror:

Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: knapsacproject.algorithm.geneticAlgorithm([I[IIII)[I 
at knapsacproject.algorithm.geneticAlgorithm(Native Method) 
at knapsacproject.algorithm.getResult(algorithm.java:39) 

我的代码:

package knapsacproject; 
public class algorithm { 


public native int [] geneticAlgorithm(int[] cost, int[] profit,int cmax, int gens, int turns); 

static { 
try { 
      System.load("C:/Users/Desktop/dp/KnapSacProject/src/knapsacproject/helo.dll"); 
    System.out.println("loaded successfully"); 
} catch (Exception e){ 
e.printStackTrace(); 
} 
} 
protected int[] cost, profit, result; 
protected int gens, turns, cmax; 

public algorithm(int[] cost,int[] profit, int gens ,int turns , int cmax) { 
this.cost=cost; 
this.profit=profit; 
this.gens=gens; 
this.turns=turns; 
    this.cmax=cmax; 

    } 

    public int[] getResult(){ 
return geneticAlgorithm(cost,profit,gens,turns,cmax); 
    } 
    public static void main (String[] args) { 
    } 
    } 

那么如何我返回多个值或如何解决这个问题?

+1

那......甚至没有像Java代码,给我。我认为你是在混合类和函数,Java保持两个不同的域......像this.cost这样的行将引用自己的类。如果你想返回多个值,您可能需要返回具有这些值的字段类,或建立他们的数组(如果他们是同一类型的。)的 –

+0

可能的复制[如何返回从多个对象Java方法?(http://stackoverflow.com/questions/457629/how-to-return-multiple-objects-from-a-java-method) –

+1

如果你希望人们看你的代码,至少使一些努力以可读的方式对其进行格式化。 –

回答

1

让你需要的返回值的数目的对象。用你想要的值创建一个新的对象。返回对象。

+0

好吧,我试试谢谢 – petko

+0

@petko让我知道如果你需要帮助,当然:) –

+0

,再次 – petko