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

一曲冷凌霜 提交于 2019-11-27 03:52:44

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.

If you don't mind using a 3rd party library, commons-lang has the ArrayUtils type with various methods for manipulation.

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

There is also the complementary method

doubles = ArrayUtils.toObject(d);

Edit: To answer the rest of the question. There will be some overhead to doing this, but unless the array is really big you shouldn't worry about it. Test it first to see if it is a problem before refactoring.

Implementing the method you had actually asked about would give something like this.

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

In Java 8, this is one-liner:

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.

You could use a for each loop to construct a temp array of the same size then cast each individual element to double and but it 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 am dead wrong here.

If you wanted to return a double[], you would need to create a new double[], populate it, and return that.

That may be a good architecture decision. First, it doesn't make a lot of sense to cast an Object[] to a Double[]; it's not really an array of Double because there could be Objects in it too. Second, if you return the array directly, the user code can modify it and alter the internal structure of your object.

The main performance impact would be in returning an array of double[], due to unboxing and the cost of allocation.

I don't have anything to add to the real question beyond what jjnguy and Eric Koslow said.

But just a side note: You mention casting an Object array to a 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 throw a class cast exception. Even though every element in the array is indeed a Double, the array was created as an array of Objects, not an array of Doubles, so the cast is invalid.

You can utilise the ArrayUtils to convert

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

No need of looping the entire data.

I would second the ArrayUtils answer and add that the 1.5 autoboxing documentation(via) kinda reveals that there is no builtin way:

There is no permitted conversion from array type SC[] to array type TC[] if there is no permitted conversion other than a string conversion from SC to TC

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!