简体   繁体   English

将2d int数组转换为2d double的最短方法

[英]Shortest way to cast a 2d int array into 2d double

I have a method which only gets double[][] to which I would like to pass int[][] , is there a short way of doing this in java, something as simple as: 我有一个方法只能得到我希望传递int[][] double[][] ,在java中有这么简单的方法,简单如下:

int [][] iArray = {
          { 1, 2, },
          { 5, 6, }
        };
double [][] dArray = (double[][]) iArray ; ???

Unfortunately the only way to cast your array is to iterate through each element and cast them one by one while re-inserting in your new double[][] array. 不幸的是,构建数组的唯一方法是迭代遍历每个元素并逐个转换它们,同时重新插入新的double[][]数组。

There is no shortcut. 没有捷径。

No, that is not correct typing. 不,这不是正确的打字。 int[] is a type and double[] is a type and they have no relation, so such assignments are not allowed. int []是一个类型,double []是一个类型,它们没有关系,所以不允许这样的赋值。 Therefore, there is no way to cast this. 因此,没有办法施展这个。

You will have to copy the elements (you can assign an int to a double without casting). 您将不得不复制元素(您可以在不转换的情况下将int分配给double)。

Hmm, is my code written in Java 8 worth being called the shortcut ? 嗯,我用Java 8编写的代码值得被称为快捷方式吗?

import java.util.Arrays;

// ...

/// Cast!
int[][] int2DArray = {{3, 1}, {3, 3, 7}, {}};
Object[] arrayOfUntypedArraies = Arrays.stream(int2DArray).map(intArray -> Arrays.stream(intArray).asDoubleStream().toArray()).toArray();
double[][] double2DArray = Arrays.copyOf(arrayOfUntypedArraies, arrayOfUntypedArraies.length, double[][].class);

/// Print!
System.out.println(Arrays.deepToString(int2DArray));
// [[1, 2], [5, 6, 7], []]
System.out.println(Arrays.deepToString(double2DArray));
// [[1.0, 2.0], [5.0, 6.0, 7.0], []]

In Java 8 you can cast a 1-dimensional array of int s into an array of double s : 在Java 8中,您可以将int的1维数组转换为double s数组:

double[] singleDimensionDoubleArray = 
        Arrays.stream(singleDimensionIntArray).asDoubleStream().toArray()

I therefore wrote what I assumed would be a faster version by looping over the rows: 因此,我通过循环遍历行写了我认为更快的版本:

int[][] iArray = {
    {1, 2,},
    {5, 6,}
};

double[][] dArray = new double[iArray.length][];
for (int row = 0; row < iArray.length; row++) {
    dArray[row] = Arrays.stream(iArray[row]).asDoubleStream().toArray();
}

I was just about to post this answer but thought I'd post some metrics about how much faster it was that naively looping over every element. 我正准备发布这个答案,但我想我会发布一些关于每个元素天真循环多快的指标。 It turns out it was around 10x slower ! 事实证明它慢了大约10倍 I tested it with a 10000x1000 array of zeroes. 我用10000x1000的零数组测试了它。 I suppose it's the extra object creation that causes the slowdown. 我想这是额外的对象创建导致减速。

Unless someone else can prove otherwise, I think the simplest solution is actually the fastest: 除非别人能证明不是这样,否则我认为最简单的解决方案实际上是最快的:

for (int row = 0; row < iArray.length; row++) {
    for (int column = 0; column < iArray[0].length; column++) {
        dArray[row][column] = (double) iArray[row][column];
    }
}

You can't cast them, doubles are laid out different in memory than ints, this isn't a case of simply changing their name. 你不能施放它们,双打在内存中的排列不同于整数,这不是简单地改变它们的名字的情况。

EDIT: Only if double was a super or subclass class of int then it might be possible. 编辑:只有当双是一个超级或子类int的则是可能的。

You can do it like this: 你可以这样做:

    int[][] intarray = {{1, 2}, {5, 6}};

    double[][] doublearray = new double[intarray.length][intarray[0].length];

    for(int i = 0; i < intarray.length; i++)
    {
        for(int j = 0; j < intarray[0].length; j++)
            doublearray[i][j] = (double) intarray[i][j];
    }

Edit: as Andreas_D pointed out, this only works if all rows are of the same lenght, if you want variable lenght, you'll have to traverse the second for loop for a variable amount of columns. 编辑:正如Andreas_D所指出的,这只有在所有行都具有相同长度时才有效,如果你想要变量长度,你必须遍历第二个for循环以获得可变数量的列。

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

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