How to convert primitive double array to Double array

  • A+
Category:Languages

With the Apache common math library I get back a primitive double array.

  RealMatrix pInverse = new LUDecomposition(p).getSolver().getInverse();    double[][] temp = pInverse.getData(); 

I need to convert temp to a Double[][]

  Double[][] inverse = new Double[][]temp; 

 


It's a simple set of nested loop:

Double[][] inverse = new Double[temp.length][]; for (int i = 0; i < temp.length; i++) {     inverse[i] = new Double[temp[i].length];     for (int j = 0; j < temp[i].length; j++)         inverse[i][j] = temp[i][j]; } 

It's even shorter if you know all the sub-arrays are the same size:

Double[][] inverse = new Double[temp.length][temp[0].length]; for (int i = 0; i < temp.length; i++)     for (int j = 0; j < temp[0].length; j++)         inverse[i][j] = temp[i][j]; 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: