-1

I have the following data structure in Linear_Programming class .

ArrayList<ArrayList<Double>> constraint_temp  ;
ArrayList<Double> B_value_temp;
ArrayList<Double> obj_func ; 

I want to pass this object to Simplex class constructor by the following code .

 Simplex smlpx = new Simplex(constraint_temp, B_value_temp,obj_func);

The prototype for constructor of Simplex method is as follows :

 public Simplex(double[][] A, double[] b, double[] c);

So I need a way to convert the arraylist to array . How can I do this ? Please suggest me a way .

gprathour
  • 14,813
  • 5
  • 66
  • 90
osimer pothe
  • 2,827
  • 14
  • 54
  • 92

1 Answers1

3

First, write a method to convert a List<Double> to a 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;
}

Then the complicated version is the two-dimensional argument 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);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249