1. Overview
In this article, We'll learn how to create and access nested or multidimensional arrays.
Example programs on single, 2D and 3D dimensional arrays in java.
Read more on How to create and initialize an array in java?
2. Single Dimensional Arrays
An array as single dimensional can be created in many ways.
2.1 Create Array with zero size
In java, it is possible to create empty arrays.
Example 1
package com.javaprogramto.arrays.multidimensional; import org.apache.commons.lang3.ArrayUtils; public class NestedArraysExample1 { public static void main(String[] args) { int[] array1 = {}; int[] array2 = new int[0]; int[] array3 = ArrayUtils.EMPTY_INT_ARRAY; System.out.println("array1 length is "+array1.length); System.out.println("array2 length is "+array2.length); System.out.println("array3 length is "+array3.length); } }
Output
array1 length is 0 array2 length is 0 array3 length is 0
2.2 Create Array With Default or Same Values
Array values can be accessed and filled with the default or custom values.
We can access and update the array values based on its index.
Look at the below example. All arrays are initialized with 0 as the default value but array3 is initialized with 5 using Arrays.fill() method.
And all array 0 index value accessed as array[index].
Example 2
package com.javaprogramto.arrays.multidimensional; import java.util.Arrays; import java.util.Collections; public class NestedArraysExample2 { public static void main(String[] args) { int[] array1 = { 0, 0 }; int[] array2 = new int[5]; int[] array3 = new int[5]; Arrays.fill(array3, 5); Integer[] array4 = Collections.nCopies(array3.length, 0).toArray(new Integer[0]); System.out.println("array1[0] value " + array1[0]); System.out.println("array2[0] value " + array2[0]); System.out.println("array3[0] value " + array3[0]); System.out.println("array4[0] value " + array4[0]); } }
Output
array1[0] value 0 array2[0] value 0 array3[0] value 5 array4[0] value 0
3. Java Nested Arrays With 2D Array Examples
In java, we can create the two dimensional array to store the different types of data such as matrix,
2D array is a grid representation and data values are organized as rows.
Each row is formed with single dimension array.
3.1 2D Array Creation
Two dimensional array can be created in the following ways.
array1 is 0 sized array and array2 is with size of 5. array2 is initialzed with value 0 as default.
Example 3
public class NestedArraysExample3 { public static void main(String[] args) { int[][] array1 = { }; int[][] array2 = new int[5][5]; System.out.println("array1 length " + array1.length); System.out.println("array2 length " + array2.length); } }
Output
array1 length 0 array2 length 5
3.2 2D Array Assign Values
Assigning the values to two dimensional array.
a) Values can be assinged while creating an array
b) First create an array with default values and next assing values using for loop.
Example
package com.javaprogramto.arrays.multidimensional; public class NestedArraysExample3 { public static void main(String[] args) { // Assigning values when array is getting created int[][] array1 = { { 1, 2, 3 }, { 4, 5, 6 } }; // Create an array first and then next assing values using for loop. int[][] array2 = new int[5][5]; int count = 1; for (int i = 0; i < array2.length; i++) { for (int j = 0; i < array2[0].length; j++) { array2[i][j] = count; count++; } } } }
3.3 2D Array Accessing Values
2D Array nested values are access by the index and iterate through nested for loops.
Example
System.out.println("2D array values by index"); System.out.println("array1[1][1] - "+array1[1][1]); System.out.println("array2[2][2] - "+array2[2][2]); System.out.println("array1 full valus"); for (int i = 0; i < array1.length; i++) { for (int j = 0; j < array1[0].length; j++) { System.out.print(array1[i][j]+" "); } System.out.println(); }
Output
2D array values by index array1[1][1] - 5 array2[2][2] - 13 array1 full valus 1 2 3 4 5 6
4. 3D or more Nested or MultiDimensinal Arrays
3D or more dimensinal arrays are much useful based on the use case.
If you want to store the 10 persons peformance stats in 2 tests which is repeated twice a month and this has to be captured for 6 months.
In such cases we can not use the traditional List or other collections.
Declare an array 4 diminsional as below.
double[][][][] array = new double [12][10][2][2];
3D or more nested arrays creation, setting values and accessing them is similar to the 2D arrays.
4.1 Creating 3D Nested Array
Example
double[][][][] array = new double[12][10][2][2]; int[][][] array1 = { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 6, 7, 8 }, { 9, 10, 11 } } }; int[][][] array2 = new int[5][5][5];
4.2 Assing values to 3D nested array
Example
// Assigning values when array is getting created int[][][] array1 = { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 6, 7, 8 }, { 9, 10, 11 } } }; // Create an array first and then next assign values using for loop. int[][][] array2 = new int[5][5][5]; int count = 1; for (int i = 0; i < array2.length; i++) { for (int j = 0; j < array2[0].length; j++) { for (int k = 0; k < array2[0][0].length; k++) { array2[i][j][k] = count; count++; } } }
4.3 Accessing 3D Nested Array values
Example
System.out.println("3D array values by index"); System.out.println("array1[1][1] - " + array1[1][1][1]); System.out.println("array2[2][2] - " + array2[2][2][2]); System.out.println("\n3D array1 full values"); for (int i = 0; i < array1.length; i++) { for (int j = 0; j < array1[0].length; j++) { System.out.print("{ "); for (int k = 0; k < array1[0][0].length; k++) { System.out.print(array1[i][j][k] + " "); } System.out.print(" }"); } System.out.println(); }
4.4 3D Nested Array Full Code
Example
public class NestedArraysExample4 { public static void main(String[] args) { double[][][][] array = new double[12][10][2][2]; // Assigning values when array is getting created int[][][] array1 = { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 6, 7, 8 }, { 9, 10, 11 } } }; // Create an array first and then next assign values using for loop. int[][][] array2 = new int[5][5][5]; int count = 1; for (int i = 0; i < array2.length; i++) { for (int j = 0; j < array2[0].length; j++) { for (int k = 0; k < array2[0][0].length; k++) { array2[i][j][k] = count; count++; } } } System.out.println("3D array values by index"); System.out.println("array1[1][1] - " + array1[1][1][1]); System.out.println("array2[2][2] - " + array2[2][2][2]); System.out.println("\n3D array1 full values"); for (int i = 0; i < array1.length; i++) { for (int j = 0; j < array1[0].length; j++) { System.out.print("{ "); for (int k = 0; k < array1[0][0].length; k++) { System.out.print(array1[i][j][k] + " "); } System.out.print(" }"); } System.out.println(); } } }
Output
3D array values by index array1[1][1] - 10 array2[2][2] - 63 3D array1 full values { 1 2 3 }{ 4 5 6 } { 6 7 8 }{ 9 10 11 }
5. Conclusion
In this article, we've seen how to create nested array in java. Examples on 1D, 2D and 3D dimensional arrays.
No comments:
Post a Comment
Please do not add any spam links in the comments section.