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

发布于 2024-07-26 10:31:43 字数 768 浏览 8 评论 0原文

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

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[]< /代码>。

所以我有两个问题:

  1. 有什么方法可以从 Object[][] 表中获取列数据并返回基元数组?
  2. 如果我确实将接口更改为返回 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:

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

苄①跕圉湢 2024-08-02 10:31:43

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

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

还有补充方法

doubles = ArrayUtils.toObject(d);

编辑:回答问题的其余部分。 这样做会产生一些开销,但除非数组真的很大,否则您不必担心。 重构之前先测试一下是否有问题。

实现您实际询问的方法会给出类似的结果。

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

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]);
}
雨的味道风的声音 2024-08-02 10:31:43

在 Java 8 中,这是单行:

Double[] boxed = new Double[] { 1.0, 2.0, 3.0 };
double[] unboxed = Stream.of(boxed).mapToDouble(Double::doubleValue).toArray();

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

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.

执笏见 2024-08-02 10:31:43

不幸的是,如果您想将其转换为 double[],则需要循环遍历整个列表并取消对 Double 的装箱。

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

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.

冰雪之触 2024-08-02 10:31:43

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

double[] tempArray = new double[data[columnIndex].length];
int i = 0;
for(Double d : (Double[]) data[columnIndex]) {
  tempArray[i] = (double) d;
  i++;
}

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

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.

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.

温馨耳语 2024-08-02 10:31:43

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

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

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

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.

白衬杉格子梦 2024-08-02 10:31:43

您可以利用 ArrayUtils 进行转换,

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

无需循环整个数据。

You can utilise the ArrayUtils to convert

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

No need of looping the entire data.

烟雨凡馨 2024-08-02 10:31:43

除了 jjnguy 和 Eric Koslow 所说的之外,我没有什么可以补充的。

但请注意:您提到将 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;

最后一行将抛出类转换异常。 尽管数组中的每个元素确实都是 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:

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.

趴在窗边数星星i 2024-08-02 10:31:43

我会赞同 ArrayUtils 的答案并添加 1.5 自动装箱文档(via) 有点揭示了没有内置方法:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文