[英]How to get Array from Arraylist
我在Linear_Programming類中具有以下數據結構。
ArrayList<ArrayList<Double>> constraint_temp ;
ArrayList<Double> B_value_temp;
ArrayList<Double> obj_func ;
我想通過以下代碼將此對象傳遞給Simplex類構造函數。
Simplex smlpx = new Simplex(constraint_temp, B_value_temp,obj_func);
Simplex方法的構造函數的原型如下:
public Simplex(double[][] A, double[] b, double[] c);
所以我需要一種將arraylist轉換為array的方法。 我怎樣才能做到這一點 ? 請給我建議一種方法。
首先,編寫一種將List<Double>
轉換為double[]
-
private static double[] fromList(List<Double> al) {
double[] out = new double[al.size()];
for (int i = 0;i < al.size(); i++) {
out[i] = al.get(i);
}
return out;
}
那么復雜的版本就是二維參數a
,
double[][] a = new double[constraint_temp.size()][];
for (int i = 0; i < a.length; i++) {
a[i] = fromList(constraint_temp.get(i));
}
double[] b = fromList(B_value_temp);
double[] c = fromList(obj_func);
Simplex smlpx = new Simplex(a,b,c);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.