2016-03-23 177 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