How to convert Double [] to double []?

I am implementing an interface that has functionality similar to a table that can contain types of objects. The interface defines the following function:

double[] getDoubles(int columnIndex); 

Where I am at a dead end is that in my implementation I store table data in an array of 2D Object ( Object[][] data ). When I need to return the values, I want to do the following (it is assumed that getDoubles() will only be called in a column containing paired ones, so there will be no ClassCastExceptions ):

 double[] getDoubles(int columnIndex) { return (double[]) data[columnIndex]; } 

But - Java does not allow Object[] be different from double[] . Dropping it to double[] fine because Double is an object, not a primitive, but my interface indicates that the data will be returned as double[] .

I have two questions:

  • Is there a way to get the column data from the Object[][] table and return an array of primitives?
  • If I change the interface to return double[] , will there be any performance impact?
+54
java arrays autoboxing
Jul 10 '09 at 14:46
source share
8 answers

Unfortunately, you will need to iterate over the entire list and unzip Double if you want to convert it to double[] .

In terms of performance, there is some time in Java related to boxing and unboxing primitives. If the set is small enough, you will not see any performance issues.

+31
Jul 10 '09 at 14:52
source share

If you do not mind using a third-party library, commons-lang is of type ArrayUtils with various manipulation methods.

 Double[] doubles; ... double[] d = ArrayUtils.toPrimitive(doubles); 

There is also an additional method

 doubles = ArrayUtils.toObject(d); 

Edit: answer the remaining question. There will be some overhead for this, but if the array is really large, you should not worry about that. Check it out first to make sure this is a problem before refactoring.

The implementation of the method that you really asked about will produce something like this.

 double[] getDoubles(int columnIndex) { return ArrayUtils.toPrimitive(data[columnIndex]); } 
+74
Jul 10 '09 at 15:09
source share

In Java 8, this is single-line:

 Double[] boxed = new Double[] { 1.0, 2.0, 3.0 }; double[] unboxed = Stream.of(boxed).mapToDouble(Double::doubleValue).toArray(); 

Note that this still iterates over the original array and creates a new one.

+28
May 8 '15 at 6:55
source share

You can use a for each loop to build a temp array of the same size, and then use each individual element for a two, but in the array.

SO:

 double[] tempArray = new double[data[columnIndex].length]; int i = 0; for(Double d : (Double) data[columnIndex]) { tempArray[i] = (double) d; i++; } 

Please correct me if I'm wrong here.

+8
Jul 10 '09 at 14:54
source share

If you want to return double[] , you will need to create a new double[] , fill it in and return.

This may be a good architecture solution. Firstly, it does not matter much to give Object[] a double[] ; it's not really a Double array, because it can have an Object . Secondly, if you immediately return an array, the user code can change it and change the internal structure of your object.

The main performance impact will be the return of the double[] array due to unpacking and allocation costs.

+3
Jul 10 '09 at 14:53
source share

I have nothing to add to the real question, other than what jjnguy and Eric Koslow said.

But just a note: you indicate that the Object array is used for the Double array. The following will not work:

 Object[] oa=new Object[3]; oa[0]=new Double("1.0"); oa[1]=new Double("2.0"); oa[2]=new Double("3.0"); Double[] da=(Double[])oa; 

The last line will select a class exception. Despite the fact that each element of the array is indeed Double, the array was created as an array of objects, not an array of double digits, so the cast is not valid.

+1
Jul 10 '09 at 16:56
source share

You can use ArrayUtils to convert

 Double[] d; // initialise double[] array = ArrayUtils.toPrimitive(d); 

No need to sort through all the data.

+1
Feb 15 '17 at 13:28
source share

I would answer the ArrayUtils question and add that 1.5 documentation on auto-update ( via ), the view shows that there is no built-in way:

There is no permitted conversion from an array type SC [] to an array type TC [], unless there is an allowed conversion other than converting strings from SC to TC

0
Jul 10 '09 at 16:33
source share



All Articles