How to Convert Double[] to Double[]

How do I convert Double[] to double[]?

Unfortunately you will need to loop through the entire list and unbox the Double if you want to convert it to a double[].

As far as performance goes, there is some time associated with boxing and unboxing primitives in Java. If the set is small enough, you won't see any performance issues.

How to convert a Double[] to double...?

double[] unboxed = Stream.of(boxed).mapToDouble(Double::doubleValue).toArray();

Credits to: https://stackoverflow.com/a/30117592/3635454

How to convert primitive double array to Double array

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];

Converting double[] to Double in java

You can not cast double[] to an Double, because of the obvious reason that they are just not COMPATIBLE.

If you are storing the double values in double[] and want to convert individual double to Double, you need not to typecast explicitly. Autoboxing works by default.

double[] val = {1.01, 3.09};

for(Double d : val){
// Make use of Double d here
}

How to convert queuedouble[] to double[][] in java?

You can use this method queue.toArray(new Double[0][0]);.

Be careful.Do not use nonparametric overloaded method queue.toArray().Because it can't convert to Double[][] but Obejct[].

For example:

LinkedBlockingQueue<Double[]> linkedBlockingQueue = new LinkedBlockingQueue<>();
linkedBlockingQueue.put(new Double[]{1.1, 1.2});
linkedBlockingQueue.put(new Double[]{2.1, 2.2});
Double[][] doubleTwoDimensionArray = linkedBlockingQueue.toArray(new Double[0][0]);

The new Double[0][0] only be used to appoint the type of conversion.

How to cast from ListDouble to double[] in Java?

High performance - every Double object wraps a single double value. If you want to store all these values into a double[] array, then you have to iterate over the collection of Double instances. A O(1) mapping is not possible, this should be the fastest you can get:

 double[] target = new double[doubles.size()];
for (int i = 0; i < target.length; i++) {
target[i] = doubles.get(i).doubleValue(); // java 1.4 style
// or:
target[i] = doubles.get(i); // java 1.5+ style (outboxing)
}

Thanks for the additional question in the comments ;) Here's the sourcecode of the fitting ArrayUtils#toPrimitive method:

public static double[] toPrimitive(Double[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_DOUBLE_ARRAY;
}
final double[] result = new double[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i].doubleValue();
}
return result;
}

(And trust me, I didn't use it for my first answer - even though it looks ... pretty similiar :-D )

By the way, the complexity of Marcelos answer is O(2n), because it iterates twice (behind the scenes): first to make a Double[] from the list, then to unwrap the double values.

How to convert double[] to Listdouble[] in Java?

public List<double[]> toListTuple(double [] array){
List<double []> ret = new ArrayList<double[]>() ;

double [] sublist = null;
for(int i = 0;i<array.length;i++){
if(sublist == null)
sublist = new double[3];
sublist[i%3] = array[i];
if(i%3==2) {
ret.add(sublist);
sublist = null;
}
}

if(sublist!=null){
ret.add(sublist); // This means some of the last elements weren't initialized
}
return ret;
}


Related Topics



Leave a reply



Submit