$show=/label

Java Nested Arrays - Multidimensional Arrays Examples

SHARE:

A quick guide to create and access nested or multidimensional arrays in java with examples.

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.


Java Nested Arrays - Multidimensional Arrays Examples



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.



COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java Nested Arrays - Multidimensional Arrays Examples
Java Nested Arrays - Multidimensional Arrays Examples
A quick guide to create and access nested or multidimensional arrays in java with examples.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKoEwC63n_SxuTjMggRmYSmlE1fAsjdzniZbbevkNy1gSlcabmObHrfd4-NXvC6XxaksH0dEcSMVhjamoe2uECzLmvTCNFDsoo99qYk9d-7c2a0RJnjljfKdC4RRQUQmSjH7shlxFyn1g/w640-h438/Java+Nested+Arrays+-+Multidimensional+Arrays+Examples.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKoEwC63n_SxuTjMggRmYSmlE1fAsjdzniZbbevkNy1gSlcabmObHrfd4-NXvC6XxaksH0dEcSMVhjamoe2uECzLmvTCNFDsoo99qYk9d-7c2a0RJnjljfKdC4RRQUQmSjH7shlxFyn1g/s72-w640-c-h438/Java+Nested+Arrays+-+Multidimensional+Arrays+Examples.png
JavaProgramTo.com
https://www.javaprogramto.com/2021/11/java-nested-arrays.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2021/11/java-nested-arrays.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content