Java 如何将 Double[] 转换为 double[]?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1109988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:53:10  来源:igfitidea点击:

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

javaarraysautoboxing

提问by Brian

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

我正在实现一个接口,它的功能类似于可以包含对象类型的表。该接口指定了以下功能:

double[] getDoubles(int columnIndex);

Where I'm stumped is that in my implementation, I'm storing the table data in a 2D Objectarray (Object[][] data). When I need to return the values, I want to do the following (it is assumed that getDoubles()will only be called on a column that contains doubles, so there will be no ClassCastExceptions):

我难住的地方是,在我的实现中,我将表数据存储在一个二维Object数组 ( Object[][] data) 中。当我需要返回值时,我想执行以下操作(假设getDoubles()只会在包含双精度的列上调用,因此不会有ClassCastExceptions):

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

But - Java doesn't allow Object[]to be cast to double[]. Casting it to Double[]is ok because Doubleis an object and not a primitive, but my interface specifies that data will be returned as a double[].

但是 - Java 不允许Object[]强制转换为double[]. 将它转换Double[]为可以,因为它Double是一个对象而不是原始类型,但我的接口指定数据将作为double[].

So I have two questions:

所以我有两个问题:

  1. Is there any way I can get the column data out of the Object[][]table and return the array of primitives?
  2. If I do change the interface to return Double[], will there be any performance impact?
  1. 有什么办法可以从Object[][]表中取出列数据并返回原语数组?
  2. 如果我确实将接口更改为 return Double[],是否会影响性能?

采纳答案by jjnguy

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

不幸的是,Double如果要将其转换为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.

就性能而言,Java 中的装箱和拆箱原语需要一些时间。如果集合足够小,您将不会看到任何性能问题。

回答by John Calsbeek

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

如果你想返回 a double[],你需要创建一个new double[],填充它,然后返回它。

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 Doublebecause 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.

这可能是一个很好的架构决策。首先,将 an 转换Object[]为 a没有多大意义Double[];它并不是真正的数组,Double因为其中也可能有Objects。其次,如果直接返回数组,用户代码可以修改它并改变对象的内部结构。

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

double[]由于拆箱和分配成本,主要的性能影响将是返回 的数组。

回答by Eric Koslow

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.

您可以使用 for each 循环来构造一个相同大小的临时数组,然后将每个单独的元素转换为 double ,但它在数组中。

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.

如果我在这里错了,请纠正我。

回答by Rich Seller

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

如果您不介意使用第 3 方库,commons-lang 具有 ArrayUtils 类型,其中包含各种操作方法。

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

回答by svrist

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

我会支持 ArrayUtils 的回答,并补充说 1.5自动装箱文档via)有点揭示没有内置方式:

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

如果除了从 SC 到 TC 的字符串转换之外没有允许的转换,则不允许从数组类型 SC[] 到数组类型 TC[] 的转换

回答by Jay

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

除了 jjnguy 和 Eric Koslow 所说的之外,我对真正的问题没有任何补充。

But just a side note: You mention casting an Object array to a Double array. The following will NOT work:

但只是一个旁注:您提到将 Object 数组转换为 Double 数组。以下将不起作用:

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.

最后一行将抛出类转换异常。即使数组中的每个元素确实都是 Double ,但该数组是作为 Object 数组创建的,而不是 Double 数组,因此转换无效。

回答by Infeligo

In Java 8, this is one-liner:

在 Java 8 中,这是单行的:

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.

请注意,这仍然会遍历原始数组并创建一个新数组。

回答by Praveen

You can utilise the ArrayUtils to convert

您可以利用 ArrayUtils 进行转换

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

No need of looping the entire data.

无需循环整个数据。