简体   繁体   English

如何将二维数组展平为一维数组?

[英]How to flatten 2D array to 1D array?

How can I flatten the 2 dimensions array int originalArray[][] to 1 dimension array?如何将二维数组int originalArray[][]展平为一维数组?

    int a [] = {1,2,6,7,2};
    int b [] = {2,44,55,2};
    int c [] = {2,44,511,33};

    int originalArray [][] = new int[][]{a,b,c};

With Guava , you can use either使用Guava ,您可以使用

int[] all = Ints.concat(originalArray);

or或者

int[] all = Ints.concat(a, b, c);

With Java 8 you can "flatMap" the inner arrays:使用 Java 8,您可以“flatMap”内部数组:

int[] flatArray = Arrays.stream(originalArray)
        .flatMapToInt(Arrays::stream)
        .toArray();

or:或者:

int[] flatArray = Stream.of(a, b, c)
        .flatMapToInt(Arrays::stream)
        .toArray();

A simple for loop will do, it is not difficult, but will depend on the order in which you want to copy the values.一个简单的 for 循环就可以了,这并不困难,但取决于您想要复制值的顺序。 For instance (based on the fact that in your example the arrays all have the same length):例如(基于在您的示例中数组都具有相同长度的事实):

int[] newArray = new int[3 * a.length];
int index = 0;
for (int n = 0; n < a.length; n++) {
    newArray[index++] = a[n];
    newArray[index++] = b[n];
    newArray[index++] = c[n];
}

or (different order, a, b, c can be of different lengths):或(不同的顺序,a、b、c 可以是不同的长度):

int[] newArray = new int[a.length + b.length + c.length];
System.arraycopy(a, 0, newArray, 0, a.length);
System.arraycopy(b, 0, newArray, a.length, b.length);
System.arraycopy(c, 0, newArray, a.length + b.length, c.length);
int[] oneDArray = new int[arr.length*arr.length];
    //Flatten 2D array to 1D array...
    int s = 0;
    for(int i = 0; i < arr.length; i ++) 
          for(int j = 0; j < arr.length; j ++){                           
              oneDArray[s] = arr[i][j];
              s++;
          } 

There will be 2 steps:会有2个步骤:

1) find out total number of elements to create a new vector (1d array) 1) 找出创建新向量的元素总数(一维数组)

2) iterate through your 2d array in predefined order and copy its elements to the created vector 2)以预定义的顺序遍历您的二维数组并将其元素复制到创建的向量

int elementsNumber = 0;

for (int i = 0; i < originalArray.length; i++) {
   elementsNumber += originalArray[i].length;
}

int[] newArray = new int[elementsNumber];
int j = 0;
for (int i = 0; i < originalArray.length; i++) {
   System.arrayCopy (originalArray[i], 0, newArray, j, originalArray[i].length);
   j += originalArray[i].length;
}

Since arrays can't be extended (ie you have to declare the size of an error upon initialization), you have to traverse the arrays twice:由于数组不能扩展(即你必须在初始化时声明错误的大小),你必须遍历数组两次:

int size = 0;
for (int[] ar : originalArray) size += ar.length;
int[] result = new int[size];
int pos = 0;
for (int[] ar : originalArray) {
    System.arraycopy(ar, 0, result, pos, ar.length);
    pos += ar.length;
}

one-liner with IntStreamIntStream

IntStream.concat(
    IntStream.concat( IntStream.of(originalArray[0]), IntStream.of(originalArray[1]) ),
        IntStream.of(originalArray[2]) ).toArray();

gets: [1, 2, 6, 7, 2, 2, 44, 55, 2, 2, 44, 511, 33]得到: [1, 2, 6, 7, 2, 2, 44, 55, 2, 2, 44, 511, 33]

below code can merge varied 2D arrays (diff sizes of internal array) into a one dimensional array:下面的代码可以将不同的二维数组(内部数组的不同大小)合并为一个一维数组:

 public static Integer[] merge2DArrays(int[][] twoDArray){
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < twoDArray.length; i++) {
            for (int j = 0; j < twoDArray[i].length; j++) {
                list.add(twoDArray[i][j]);
            }
        }
    return list.toArray(new Integer[list.size()]);
    }

Count the total number of elements in originalArray.计算 originalArray 中元素的总数。 Create new array of that length.创建该长度的新数组。 Copy elements one by one into the new array.将元素一一复制到新数组中。

I am unfamiliar with any library function to do so.我不熟悉这样做的任何库函数。

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

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