如何转换双[]翻番[]? [英] How do I convert Double[] to double[]?
问题描述
我实施具有类似于可以包含一个类型的对象的表功能的接口。接口规定了以下功能:
双击[] getDoubles(INT参数:columnIndex);
在哪里我难倒的是,在我的实现,我存储在2D 对象表数据
阵列(对象[] [ ]数据
)。当我需要返回的值,我想要做以下(假设getDoubles()将只包含双打一列被调用,所以不会有ClassCastExceptions异常):
双击[] getDoubles(INT参数:columnIndex){
返回(双[])的数据[参数:columnIndex]
}
但是 - Java不允许对象[]被丢加倍[]。它铸造到双[]是可以的,因为双击
是一个对象,而不是一个原始的,但我的接口指定该数据将作为返回双[ ]
。
所以,我有两个问题:
- 有什么办法,我可以得到列的数据出了对象[] []表中,并返回原语数组?
- 如果我更改接口以双倍返还[],会有任何性能影响?
不幸的是,你会需要遍历整个列表,然后拆箱双击
,如果你想将其转换到双[]
。
至于性能也越高,有在Java装箱和拆箱图元相关的一些时间。如果集合足够小,你不会看到任何性能问题。
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
). 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):
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 Double
is an object and not a primitive, but my interface specifies that data will be returned as a double[]
.
So I have two questions:
- Is there any way I can get the column data out of the Object[][] table and return the array of primitives?
- If I do change the interface to return Double[], will there be any performance impact?
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.
这篇关于如何转换双[]翻番[]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!