-1

let's say I have a two-dimensional array with int values, and I want to fill a one-dimensional array with all the int values from the two-dimensional array, how can I do such a thing?

This is what I tried, but something is going wrong and I don't know what..

int[][] twoDimensionalArray = {{5, 2, 3, 1},
                               {4, 2, 6, 9},
                               {8, 9, 1, 8}};
int[] oneDimensionalArray = new int[twoDimensionalArray.length * twoDimensionalArray.length];
for (int i = 0; i < twoDimensionalArray.length; i++) {
    for (int j = 0; j < twoDimensionalArray.length; j++) {
      oneDimensionalArray[i] = twoDimensionalArray[i][j];
     }
}

Thank you!

Voiceeeeee
  • 91
  • 1
  • 1
  • 8

4 Answers4

1

I can suggest something like this,

String[][] my2Darr = {{5, 2, 3, 1},
                      {4, 2, 6, 9},
                      {8, 9, 1, 8}};
    List<String> list = new ArrayList<>();
    for(int i = 0; i < my2Darr.length; i++) {
        list.addAll(Arrays.asList(my2Darr[i])); // java.util.Arrays
    }
    String[] my1Darr = new String[list.size()];
    my1Darr = list.toArray(my1Darr);

Java 8:

int[][] 2darr = {{5, 2, 3, 1},
               {4, 2, 6, 9},
               {8, 9, 1, 8}};
int[] 1darr = Stream.of(2darr ).flatMapToInt(IntStream::of).toArray();
System.out.println(Arrays.toString(1darr ));
Sohan
  • 6,252
  • 5
  • 35
  • 56
  • Ended up using this, thank you. Can I ask why you only had to use one for loop to iterate through the 2 dimensional array, not 2? – Voiceeeeee Mar 01 '19 at 10:02
  • 1
    @Voiceeeeee he adds one dimension of the 2D array at a time to the ArrayList, by converting it to an appropriate List format. – Mwak Mar 01 '19 at 10:16
1
int[][] twoDimensionalArray = {{5, 2, 3, 1},
                               {4, 2, 6, 9},
                               {8, 9, 1, 8}};
int[] oneDimensionalArray = new int[twoDimensionalArray.length * twoDimensionalArray.length];
for (int i = 0; i < twoDimensionalArray.length; i++) {
    for (int j = 0; j < twoDimensionalArray[0].length; j++) {
      oneDimensionalArray[i] = twoDimensionalArray[i][j];
     }
}

Your solution was almost correct. You have to correct twoDimensionalArray.length to twoDimensionalArray[0].length. Since you want to iterate horizonally and vertically. If you iterate 2 times over twoDimensionalArray.length, you will iterate 2 times the length of the horizontal axe.

dnsiv
  • 520
  • 4
  • 20
0

You can do it using stream, e.g.:

int[][] twoDimensionalArray = {{5, 2, 3, 1},
        {4, 2, 6, 9},
        {8, 9, 1, 8}};

int[] oneDimensionalArray = Arrays.stream(twoDimensionalArray)
    .flatMapToInt(e -> Arrays.stream(e))
    .toArray();

System.out.println(Arrays.toString(oneDimensionalArray));
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
0

You can use this, which uses System.arraycopy() to copy the inner arrays into the result:

public static int[] flatten(int[][] input) {
    int length = input.length;
    if(length == 0) return new int[0];

    int[] output = new int[length * input[0].length];
    for(int i = 0; i < length; i++) {
       int[] inner = input[i];
       // 1. Parameter: the source to copy from
       // 2. Parameter: the starting index from source
       // 3. Parameter: the destionation to copy to
       // 4. Parameter: the starting index from destination
       // 5. Parameter: the amount of elements to copy
       System.arraycopy(inner, 0, output, i * length, inner.length);
    }
    return output;
}
Lino
  • 19,604
  • 6
  • 47
  • 65