Convert 1D Array into 2D Array in Java
Converting a 1D array to a 2D array entails reshaping or reorganizing the data from a one-dimensional structure into a two-dimensional one. A 1D array consists of a single line of elements, whereas a 2D array consists of rows and columns.
Example:
Input:
![Convert 1D Array into 2D Array in Java](https://static.tutorialandexample.com/java/convert-1d-array-into-2d-array-in-java1.png)
Output:
![Convert 1D Array into 2D Array in Java](https://static.tutorialandexample.com/java/convert-1d-array-into-2d-array-in-java2.png)
Explanation: To convert the 1D array [1, 2, 3, 4, 5, 6] to a 2D array with two rows and three columns, each row of the resulting matrix has three elements from the original array.
Methods for Converting 1D Array into 2D Array:
There are various methods for converting a 1D array to a 2D array; here are some methods:
- Using Java Arrays Class
- Manual Construction
- Using ArrayList of Arrays
- Using Java Streams
Using Java Arrays Class
The Java code converts a specified 1D array (arr1D) to a 2D array (arr2D) with two rows and three columns. System.arraycopy() is used to copy the 1D array's elements into the 2D array's rows.
FileName: OneDtoTwoDArray.java
import java.util.Arrays;
public class OneDtoTwoDArray {
public static void main(String[] args) {
// Original 1D array
int[] arr1D = {1, 2, 3, 4, 5, 6};
// Reshape the 1D array into a 2D array
int[][] arr2D = new int[2][3];
System.arraycopy(arr1D, 0, arr2D[0], 0, 3);
System.arraycopy(arr1D, 3, arr2D[1], 0, 3);
// Display the reshaped 2D array
System.out.println("The 2D Array is: " + Arrays.deepToString(arr2D));
}
}
Output:
The 2D Array is: [[1, 2, 3], [4, 5, 6]]
Complexity Analysis: The time complexity is O(n), where n represents the total number of entries in the one-dimensional array. The space complexity is O(1) since the reshaped 2D array is constructed in place, without the need for additional memory.
Manual Construction
The Java code manually creates a 2D array (arr2D) with dimensions of 2x3 from a 1D array (arr1D). Each element of the 2D array is assigned using stacked loops, which iterate across the rows and columns.
FileName: OneDtoTwoDArray1.java
import java.util.Arrays;
public class OneDtoTwoDArray1 {
public static void main(String[] args) {
// Original 1D array
int[] arr1D = {1, 2, 3, 4, 5, 6};
// Manual construction of a 2D array from the 1D array
int[][] arr2D = new int[2][3];
int index = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
arr2D[i][j] = arr1D[index++];
}
}
// Display the manually constructed 2D array
System.out.println("The 2D Array is: " + Arrays.deepToString(arr2D));
}
}
Output:
The 2D Array is: [[1, 2, 3], [4, 5, 6]]
Complexity Analysis: The time complexity is O(m * n), where m is the number of rows and n is the number of columns in the two-dimensional array. The space complexity equals O(1).
Using ArrayList of Arrays
The Java code divides a 1D array (arr1D) into 2D arrays (arr2D) of a specified size. Chunks are formed via arrays.copyOfRange() was used to create a list. The list of pieces is then turned into a two-dimensional array using list.toArray().
FileName: OneDtoTwoDArray2.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class OneDtoTwoDArray2 {
public static void main(String[] args) {
// Original 1D array
int[] arr1D = {1, 2, 3, 4, 5, 6};
// Create a list to store chunks of the 1D array
List<int[]> list = new ArrayList<>();
// Define the size of each chunk
int chunkSize = 3;
// Divide the 1D array into chunks and add them to the list
for (int i = 0; i < arr1D.length; i += chunkSize) {
int[] chunk = Arrays.copyOfRange(arr1D, i, Math.min(i + chunkSize, arr1D.length));
list.add(chunk);
}
// Convert the list of chunks to a 2D array
int[][] arr2D = list.toArray(new int[0][]);
// Display the 2D array
System.out.println("The 2D Array is: " + Arrays.deepToString(arr2D));
}
}
Output:
The 2D Array is: [[1, 2, 3], [4, 5, 6]]
Complexity Analysis: The time complexity is O(n), where n is the length of the one-dimensional array because each element is processed once. The space complexity is O(m * k), where m is the number of chunks and k is the chunk size, because the algorithm generates a list to store the chunks.
Using Java Streams
The Java code uses Java Streams to convert a specified 1D array (arr1D) to a 2D array (arr2D). It uses IntStream.range() to aggregate elements into a two-dimensional array with three columns, which is dynamically resized dependent on the length of the original array.
FileName: OneDtoTwoDArray3.java
import java.util.Arrays;
import java.util.stream.IntStream;
public class OneDtoTwoDArray3 {
public static void main(String[] args) {
// Original 1D array
int[] arr1D = {1, 2, 3, 4, 5, 6};
// Use Java Streams to transform 1D array to 2D array
int[][] arr2D = IntStream.range(0, arr1D.length)
.collect(() -> new int[arr1D.length / 3][3],
(acc, i) -> acc[i / 3][i % 3] = arr1D[i],
(arr1, arr2) -> {});
// Display the 2D array
System.out.println("The 2D Array is: " + Arrays.deepToString(arr2D));
}
}
Output:
The 2D Array is: [[1, 2, 3], [4, 5, 6]]
Complexity Analysis: The time complexity is O(n), where n is the length of the 1D array because each entry is handled once with Java Streams. The space complexity is O(n), which is proportional to the length of the original array and directly defines the size of the new 2D array.