0
[[4, 17, 28, 38, 43, 58, 69, 77, 83], [4, 12, 24, 35, 48, 55, 62, 73, 87], [11,
15, 22, 36, 46, 60, 67, 80, 84]]

how to convert that to this:

[4, 17, 28, 38, 43, 58, 69, 77, 83, 4, 12, 24, 35, 48, 55, 62, 73, 87, 11,
15, 22, 36, 46, 60, 67, 80, 84]

is there an easy way?

user3112115
  • 695
  • 3
  • 12
  • 23

4 Answers4

2
ArrayUtils.addAll(array1,array2)
Andrea
  • 11,801
  • 17
  • 65
  • 72
2

You can create a new array and fill it manually using a for loop as follows:

int[][] numbers = {{4, 5, 6},{3, 1, 10}, {4, 2, 9}};


ArrayList<Integer> numbers1Dim = new ArrayList<Integer>();

for (int i = 0; i < numbers.length; i++)
{
    for (int x = 0; x < numbers[i].length; x++)
    {
        numbers1Dim.add(numbers[i][x]);
    }
}
Duane
  • 1,980
  • 4
  • 17
  • 27
1

Do like this

Integer td[][]= {{4, 17, 28, 38, 43, 58, 69, 77, 83}, {4, 12, 24, 35, 48, 55, 62, 73, 87}, {11,15, 22, 36, 46, 60, 67, 80, 84}};
List<Integer> singleDArray = new ArrayList<Integer>();
for (Integer[] array :td) {         
      singleDArray.addAll(Arrays.asList(array));
}       
Integer[] sd = singleDArray.toArray(new Integer[singleDArray.size()]);
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

I would use a nested for loop:

Integer[][] array = <your array>;  
List resultList = new ArrayList();
for (int i = 0; i < array.length; i++) {  
    for (int j = 0; j < array[i].length; j++) {  
        resultList.add(array[i][j]);
    }  
}
Integer[] resultArrayList = resultList.toArray(new Integer[resultList.size()]);
System.out.println(resultArrayList);

}

Mathias G.
  • 4,875
  • 3
  • 39
  • 60