繁体   English   中英

如何从Arraylist获取数组

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM