我寫我的代碼,我想在返回多個值:如何在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) {
}
}
那麼如何我返回多個值或如何解決這個問題?
那......甚至沒有像Java代碼,給我。我認爲你是在混合類和函數,Java保持兩個不同的域......像this.cost這樣的行將引用自己的類。如果你想返回多個值,您可能需要返回具有這些值的字段類,或建立他們的數組(如果他們是同一類型的。)的 –
可能的複製[如何返回從多個對象Java方法?(http://stackoverflow.com/questions/457629/how-to-return-multiple-objects-from-a-java-method) –
如果你希望人們看你的代碼,至少使一些努力以可讀的方式對其進行格式化。 –