Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flatten 2D array to 1D array?

Tags:

java

arrays

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

    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}; 
like image 916
Jessy Avatar asked Apr 02 '10 21:04

Jessy


People also ask

Which function is most useful to convert a multidimensional array into a one dimensional array?

Flattening array means converting a multidimensional array into a 1D array. We can use reshape(-1) to do this.

How do you make an array One dimensional?

Rules for Declaring One Dimensional ArrayThe declaration must have a data type(int, float, char, double, etc.), variable name, and subscript. The subscript represents the size of the array. If the size is declared as 10, programmers can store 10 elements. An array index always starts from 0.


1 Answers

With Guava, you can use either

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

or

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

like image 59
Kevin Bourrillion Avatar answered Sep 29 '22 13:09

Kevin Bourrillion