使用System.arracopy方法int [] []到int []

时间:2018-05-25 20:07:44

标签: java arrays arraycopy

如果我想将int[][]数组转换为java中的int[],我会使用下面显示的代码。

final int width = 3, height = 4;
final int[][] source = new int[width][height];
final int[] destination = new int[width * height];
for (int x = 0; x < width; x++)
{
     for (int y = 0; y < height; y++)
     {
          destination[x * width + y] = source[x][y];
     }
}

我刚刚介绍了System.arraycopy方法。它可能是在java中复制数组的最有效方法。因此,我尝试以类似的方式实现它。

final int width = 3, height = 4;
final int[][] source = new int[width][height];
final int[] destination = new int[width * height];
for (int index = 0; index < source.length; index++)
{
    System.arraycopy(source[index], 0, destination, source[index].length, source[index].length);
}

但是,生成的数组严重失真,并且不以任何方式表示原始数组。

3 个答案:

答案 0 :(得分:1)

这就是你想要的:

final int width = 3, height = 4;
final int[][] source = new int[width][height];
final int[] destination = new int[width * height];
for (int i = 0; i < source.length; i++)
     System.arraycopy(source[i], 0, destination, i * width, height);

如果你想让它一般工作,源中每个子数组大小不同的情况,你想要这样:

int totalLength = 0;
for (int i = 0; i < source.length; i++)
   totalLength += source[i].length;
final int[] destination = new int[totalLength];
for (int len, i = 0, index = 0; i < source.length; i++, index += len)
        System.arraycopy(source[i], 0, destination, index, len = source[i].length);

答案 1 :(得分:0)

  

参数

src − This is the source array.

srcPos − This is the starting position in the source array.

dest − This is the destination array.

destPos − This is the starting position in the destination data.

length − This is the number of array elements to be copied.

https://www.tutorialspoint.com/java/lang/system_arraycopy.htm

您需要在循环中执行此操作:

final int width = 3, height = 4;
final int[][] source = new int[width][height];
final int[] destination = new int[width * height];
for (int i = 0; i < source.length; i++)
    System.arraycopy(source[i], 0, destination, i * width, source[i].length);

答案 2 :(得分:0)

此处的详细信息可能取决于数组是否为矩形。但无论如何,我建议简单地将其转换为一般实现的实用程序方法,并适用于各种输入数组。

示例如下所示。您正在寻找flatten方法。

flattenWithStreams只是一个例子,表明可以用流简洁地解决这个问题。我没有进行详细的性能分析,但使用arraycopy的方法似乎更快。

import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class FlattenArrays
{
    public static void main(String[] args)
    {
        test();
    }

    private static void test()
    {
        int[][] source = new int[3][];
        source[0] = new int[] { 0, 1, 2 };
        source[1] = new int[] { 3, 4, 5, 6 };
        source[2] = new int[] { 7, 8, 9 };

        int destinationA[] = flatten(source);
        System.out.println(Arrays.toString(destinationA));

        int destinationB[] = flattenWithStream(source);
        System.out.println(Arrays.toString(destinationB));
    }

    private static int[] flatten(int array[][])
    {
        int length = 0;
        for (int a[] : array)
        {
            length += a.length;
        }
        int destination[] = new int[length];
        int offset = 0;
        for (int a[] : array)
        {
            System.arraycopy(a, 0, destination, offset, a.length);
            offset += a.length;
        }
        return destination;
    }

    private static int[] flattenWithStream(int array[][])
    {
        return Stream.of(array).flatMapToInt(a -> IntStream.of(a)).toArray();
    }
}