如何将 Double[] 转换为 double[]?
我正在实现一个接口,其功能类似于可以包含某种类型的对象的表。 该接口指定了以下函数:
double[] getDoubles(int columnIndex);
令我困惑的是,在我的实现中,我将表数据存储在 2D Object
数组中(Object[][] data
)。 当我需要返回值时,我想要执行以下操作(假设仅在包含双精度数的列上调用 getDoubles()
,因此不会出现 ClassCastExceptions< /code>):
double[] getDoubles(int columnIndex) {
return (double[]) data[columnIndex];
}
但是 - Java 不允许将 Object[]
转换为 double[]
。 将其转换为 Double[]
是可以的,因为 Double
是一个对象而不是基元,但我的接口指定数据将以 double[]< /代码>。
所以我有两个问题:
- 有什么方法可以从
Object[][]
表中获取列数据并返回基元数组? - 如果我确实将接口更改为返回
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
). 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您不介意使用第 3 方库,commons-lang 具有 ArrayUtils 类型和各种操作方法。
还有补充方法
编辑:回答问题的其余部分。 这样做会产生一些开销,但除非数组真的很大,否则您不必担心。 重构之前先测试一下是否有问题。
实现您实际询问的方法会给出类似的结果。
If you don't mind using a 3rd party library, commons-lang has the ArrayUtils type with various methods for manipulation.
There is also the complementary method
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.
在 Java 8 中,这是单行:
请注意,这仍然会迭代原始数组并创建一个新数组。
In Java 8, this is one-liner:
Note that this still iterates over the original array and creates a new one.
不幸的是,如果您想将其转换为
double[]
,则需要循环遍历整个列表并取消对Double
的装箱。就性能而言,Java 中的装箱和拆箱原语需要花费一些时间。 如果集合足够小,您将不会看到任何性能问题。
Unfortunately you will need to loop through the entire list and unbox the
Double
if you want to convert it to adouble[]
.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.
您可以使用 for every 循环构造一个相同大小的临时数组,然后将每个单独的元素转换为 double ,但它在数组中。
如果我在这里完全错了,请纠正我。
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.
Please correct me if I am dead wrong here.
如果您想返回一个
double[]
,您需要创建一个new double[]
,填充它,然后返回它。这可能是一个很好的架构决策。 首先,将
Object[]
转换为Double[]
没有多大意义; 它实际上并不是一个Double
数组,因为其中也可能有Object
。 其次,如果直接返回数组,用户代码可以修改它并改变对象的内部结构。由于拆箱和分配成本,主要的性能影响是返回一个 double[] 数组。
If you wanted to return a
double[]
, you would need to create anew 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 aDouble[]
; it's not really an array ofDouble
because there could beObject
s 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.您可以利用 ArrayUtils 进行转换,
无需循环整个数据。
You can utilise the ArrayUtils to convert
No need of looping the entire data.
除了 jjnguy 和 Eric Koslow 所说的之外,我没有什么可以补充的。
但请注意:您提到将 Object 数组转换为 Double 数组。 以下内容将不起作用:
最后一行将抛出类转换异常。 尽管数组中的每个元素确实都是 Double,但该数组是作为对象数组创建的,而不是 Double 数组,因此强制转换无效。
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:
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.
我会赞同 ArrayUtils 的答案并添加 1.5 自动装箱文档(via) 有点揭示了没有内置方法:
I would second the ArrayUtils answer and add that the 1.5 autoboxing documentation(via) kinda reveals that there is no builtin way: