Flattening a multidimensional Java array

In Java, a multidimensional array is an array of arrays, where each element can also be an array. Sometimes we need to flatten this multidimensional array, which means converting it into a one-dimensional array. This can be useful when we want to simplify the structure of the data or when we need to apply certain operations that are only supported on one-dimensional arrays.

In this article, we will explore different ways to flatten a multidimensional Java array.

Method 1: Using nested loops

One way to flatten a multidimensional array is by using nested loops. We iterate over each element in the array and add it to a new one-dimensional array.

int[][] multidimensionalArray = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
int[] flattenedArray = new int[multidimensionalArray.length * multidimensionalArray[0].length];

int index = 0;
for (int i = 0; i < multidimensionalArray.length; i++) {
    for (int j = 0; j < multidimensionalArray[i].length; j++) {
        flattenedArray[index++] = multidimensionalArray[i][j];
    }
}

Method 2: Using recursion

Another approach to flatten a multidimensional array is by using recursion. We define a recursive function that traverses each element of the array and adds it to a new one-dimensional array.

public static int[] flattenArray(int[][] multidimensionalArray) {
    int totalElements = 0;
    for (int i = 0; i < multidimensionalArray.length; i++) {
        totalElements += multidimensionalArray[i].length;
    }

    int[] flattenedArray = new int[totalElements];
    flattenArray(multidimensionalArray, flattenedArray, 0, 0);
    return flattenedArray;
}

private static int flattenArray(int[][] multidimensionalArray, int[] flattenedArray, int index, int row) {
    if (row == multidimensionalArray.length) {
        return index;
    }

    for (int i = 0; i < multidimensionalArray[row].length; i++) {
        flattenedArray[index++] = multidimensionalArray[row][i];
    }

    return flattenArray(multidimensionalArray, flattenedArray, index, row + 1);
}

Method 3: Using Java 8 Stream API

With Java 8 and the Stream API, we can use flatMap to flatten a multidimensional array in a concise way.

int[][] multidimensionalArray = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
int[] flattenedArray = Arrays.stream(multidimensionalArray)
                            .flatMapToInt(Arrays::stream)
                            .toArray();

Conclusion

Flattening a multidimensional Java array can be achieved using different approaches. Depending on the complexity of the array and personal preference, you can choose the most suitable method. Whether you use nested loops, recursion, or leverage the Stream API, the goal is to transform the multidimensional array into a one-dimensional array for easier processing and manipulation of the data.

#java #multidimensionalarray