简体   繁体   English

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

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

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 Object array ( Object[][] data ). 我感到困惑的是,在我的实现中,我将表数据存储在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 on a column that contains doubles, so there will be no ClassCastExceptions ): 当我需要返回值时,我想要执行以下操作(假设只在包含双精度的列上调用getDoubles() ,因此不会有ClassCastExceptions ):

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

But - Java doesn't allow Object[] to be cast to double[] . 但是 - Java不允许将Object[]强制转换为double[] Casting it to Double[] is ok because Double is an object and not a primitive, but my interface specifies that data will be returned as a 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? 有没有什么办法可以从Object[][]表中获取列数据并返回原语数组?
  2. If I do change the interface to return Double[] , will there be any performance impact? 如果我确实更改了界面以返回Double[] ,是否会对性能产生影响?

If you don't mind using a 3rd party library, commons-lang has the ArrayUtils type with various methods for manipulation. 如果您不介意使用第三方库,则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]);
}

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. 请注意,这仍然会遍历原始数组并创建一个新数组。

Unfortunately you will need to loop through the entire list and unbox the Double if 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. 就性能而言,Java中的装箱和拆箱原语有一些时间。 If the set is small enough, you won't see any performance issues. 如果该集合足够小,您将看不到任何性能问题。

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. 你可以使用a 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. 如果我在这里错了,请纠正我。

If you wanted to return a double[] , you would need to create a new double[] , populate it, and return that. 如果你想返回一个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[] ; 首先,将Object[]转换为Double[]并没有多大意义; it's not really an array of Double because there could be Object s in it too. 它不是真正的Double数组,因为它也可能有Object 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. 由于拆箱和分配成本,主要的性能影响是返回double[]数组。

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. 但只是旁注:你提到将一个Object数组转换为Double数组。 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. 尽管数组中的每个元素确实都是Double,但数组是作为Objects数组创建的,而不是双打数组,因此转换无效。

You can utilise the ArrayUtils to convert 您可以使用ArrayUtils进行转换

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: 我会先回答ArrayUtils的答案,并补充一点,1.5 autoboxing文档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 []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM