如何将2D阵列展平为1D阵列?

时间:2022-12-29 18:49:50

How can I flatten the 2 dimensions array int originalArray[][] to 1 dimension array?

如何将2维数组int originalArray [] []展平为1维数组?

    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};

7 个解决方案

#1


6  

A simple for loop will do, it is not difficult, but will depend on the order you wat to copy the values. For instance (based on the fact that in your example the arrays all have the same length):

一个简单的for循环可以做,这并不困难,但取决于你要复制值的顺序。例如(基于在您的示例中数组都具有相同长度的事实):

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);

#2


39  

With Guava, you can use either

使用番石榴,您可以使用其中任何一种

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

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

or

要么

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

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

#3


23  

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();

#4


4  

There will be 2 steps:

将有两个步骤:

1) find out total number of elements to create a new vector (1d array)

1)找出创建新向量的元素总数(1d数组)

2) iterate through your 2d array in predefined order and copy its elements to the created vector

2)以预定义的顺序迭代你的2d数组并将其元素复制到创建的向量

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;
}

#5


2  

Since arrays can't be extended (i.e. 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;
}

#6


2  

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++;
          } 

#7


-1  

Count the total number of elements in originalArray. Create new array of that length. Copy elements one by one into the new array.

计算originalArray中元素的总数。创建该长度的新数组。将元素逐个复制到新数组中。

I am unfamiliar with any library function to do so.

我不熟悉任何库函数。

#1


6  

A simple for loop will do, it is not difficult, but will depend on the order you wat to copy the values. For instance (based on the fact that in your example the arrays all have the same length):

一个简单的for循环可以做,这并不困难,但取决于你要复制值的顺序。例如(基于在您的示例中数组都具有相同长度的事实):

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);

#2


39  

With Guava, you can use either

使用番石榴,您可以使用其中任何一种

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

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

or

要么

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

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

#3


23  

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();

#4


4  

There will be 2 steps:

将有两个步骤:

1) find out total number of elements to create a new vector (1d array)

1)找出创建新向量的元素总数(1d数组)

2) iterate through your 2d array in predefined order and copy its elements to the created vector

2)以预定义的顺序迭代你的2d数组并将其元素复制到创建的向量

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;
}

#5


2  

Since arrays can't be extended (i.e. 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;
}

#6


2  

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++;
          } 

#7


-1  

Count the total number of elements in originalArray. Create new array of that length. Copy elements one by one into the new array.

计算originalArray中元素的总数。创建该长度的新数组。将元素逐个复制到新数组中。

I am unfamiliar with any library function to do so.

我不熟悉任何库函数。