
- Java - Home
- Java - Overview
- Java - History
- Java - Features
- Java Vs. C++
- JVM - Java Virtual Machine
- Java - JDK vs JRE vs JVM
- Java - Environment Setup
- Java - Hello World Program
- Java - Comments
- Java - Basic Syntax
- Java - Variables
- Java - Data Types
- Java - Type Casting
- Java - Unicode System
- Java - User Input
- Java - Date & Time
Java Operators
- Java - Operators
- Java - Arithmetic Operators
- Java - Assignment Operators
- Java - Relational Operators
- Java - Logical Operators
- Java - Bitwise Operators
- Java Operator Precedence & Associativity
- Java - Unary Operators
Java Control Statements
- Java - Decision Making
- Java - If Else Statement
- Java - Switch Statement
- Java - Loop Control
- Java - For Loop
- Java - For-Each Loop
- Java - While Loop
- Java - Do While Loop
- Java - Break Statement
- Java - Continue Statement
Object Oriented Programming
- Java - OOPs Concepts
- Java - Object & Classes
- Java - Class Attributes
- Java - Class Methods
- Java - Methods
- Java - Variables Scope
- Java - Constructors
- Java - Access Modifiers
- Java - Inheritance
- Java - Aggregation
- Java - Polymorphism
- Java - Overriding
- Java - Method Overloading
- Java - Dynamic Binding
- Java - Static Binding
- Java - Instance Initializer Block
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java - Inner Classes
- Java - Static Class
- Java - Anonymous Class
- Java - Singleton Class
- Java - Wrapper Classes
- Java - Enums
- Java - Enum Constructor
- Java - Enum Strings
Java Built-in Classes
- Java - Number
- Java - Boolean
- Java - Characters
- Java - Arrays
- Java - Multi-Dimensional Arrays
- Java - Final Arrays
- Java - Math Class
Java File Handling
- Java - Files
- Java - Create a File
- Java - Write to File
- Java - Read Files
- Java - Delete Files
- Java - Directories
- Java - I/O Streams
Java Error & Exceptions
- Java - Exceptions
- Java - try-catch Block
- Java - try-with-resources
- Java - Multi-catch Block
- Java - Nested try Block
- Java - Finally Block
- Java - throw Exception
- Java - Exception Propagation
- Java - Built-in Exceptions
- Java - Custom Exception
Java Multithreading
- Java - Multithreading
- Java - Thread Life Cycle
- Java - Creating a Thread
- Java - Starting a Thread
- Java - Joining Threads
- Java - Naming Thread
- Java - Thread Scheduler
- Java - Thread Pools
- Java - Main Thread
- Java - Thread Priority
- Java - Daemon Threads
- Java - Thread Group
- Java - Shutdown Hook
Java Synchronization
- Java - Synchronization
- Java - Block Synchronization
- Java - Static Synchronization
- Java - Inter-thread Communication
- Java - Thread Deadlock
- Java - Interrupting a Thread
- Java - Thread Control
- Java - Reentrant Monitor
Java Networking
- Java - Networking
- Java - Socket Programming
- Java - URL Processing
- Java - URL Class
- Java - URLConnection Class
- Java - HttpURLConnection Class
- Java - Socket Class
- Java - Generics
Java Collections
Java Interfaces
- Java - List Interface
- Java - Queue Interface
- Java - Map Interface
- Java - SortedMap Interface
- Java - Set Interface
- Java - SortedSet Interface
Java Data Structures
Java Collections Algorithms
Advanced Java
- Java - Command-Line Arguments
- Java - Lambda Expressions
- Java - Sending Email
- Java - Applet Basics
- Java - Javadoc Comments
- Java - Autoboxing and Unboxing
- Java - File Mismatch Method
- Java - REPL (JShell)
- Java - Multi-Release Jar Files
- Java - Private Interface Methods
- Java - Inner Class Diamond Operator
- Java - Multiresolution Image API
- Java - Collection Factory Methods
- Java - Module System
- Java - Nashorn JavaScript
- Java - Optional Class
- Java - Method References
- Java - Functional Interfaces
- Java - Default Methods
- Java - Base64 Encode Decode
- Java - Switch Expressions
- Java - Teeing Collectors
- Java - Microbenchmark
- Java - Text Blocks
- Java - Dynamic CDS archive
- Java - Z Garbage Collector (ZGC)
- Java - Null Pointer Exception
- Java - Packaging Tools
- Java - Sealed Classes
- Java - Record Classes
- Java - Hidden Classes
- Java - Pattern Matching
- Java - Compact Number Formatting
- Java - Garbage Collection
- Java - JIT Compiler
Java Miscellaneous
- Java - Recursion
- Java - Regular Expressions
- Java - Serialization
- Java - Strings
- Java - Process API Improvements
- Java - Stream API Improvements
- Java - Enhanced @Deprecated Annotation
- Java - CompletableFuture API Improvements
- Java - Marker Interface
- Java - Streams
- Java - Datetime Api
- Java 8 - New Features
- Java 9 - New Features
- Java 10 - New Features
- Java 11 - New Features
- Java 12 - New Features
- Java 13 - New Features
- Java 14 - New Features
- Java 15 - New Features
- Java 16 - New Features
Java APIs & Frameworks
Java Class References
- Java - Scanner
- Java - Arrays
- Java - Strings
- Java - Date
- Java - ArrayList
- Java - Vector
- Java - Stack
- Java - PriorityQueue
- Java - LinkedList
- Java - ArrayDeque
- Java - HashMap
- Java - LinkedHashMap
- Java - WeakHashMap
- Java - EnumMap
- Java - TreeMap
- Java - IdentityHashMap
- Java - HashSet
- Java - EnumSet
- Java - LinkedHashSet
- Java - TreeSet
- Java - BitSet
- Java - Dictionary
- Java - Hashtable
- Java - Properties
- Java - Collection
- Java - Array
Java Useful Resources
Java Multi-Dimensional Arrays
Two-dimensional arrays are used to store data in rows and columns, where each row can represent a separate individual array. In short, a two-dimensional array contains one-dimensional arrays of elements.
Creating a 2D Array
In Java, we can declare a two-dimensional array in multiple ways, but the correct and most common way to declare a two-dimensional array is as follows:
data_type[][] name_of_Array = new data_type[p][q];
Where "p" represents the number of rows and "q" represents the number of columns. The total number of elements to be stored in a 2D array is equal to the product of p and q (p à q). For example:
int[][] array1 = new int [2][2]
The total number of elements to be stored in array1 is 2 Ã 2 = 4.

Initializing a 2D Array
We can add values to a two-dimensional array in three different ways, they are as follows:
Direct Initialization
In Java, we can directly initialize the values of a two-dimensional array when declaring the two-dimensional array. The following is the syntax for direct initialization of a 2D array:
dataType[][] name_of_Array = { {value_1_of_row_1, value_2_of_row_1, ...}, {value_1_of_row_2, value_2_of_row_2, ...}, ... more number rows };
By Indexing
In Java, we can initialize the values of a two-dimensional array by specifying the indexing of the elements, i.e, the row index and column index of the 2D array. The following is the syntax for initializing the value of a 2D array by indexing:
name_of_Array [rowIndex][columnIndex] = value;
Using Nested For Loops
In Java, we can initialize the values of a two-dimensional array using nested for loops, in which the first loop is used to iterate over the rows and the second is used to iterate over the columns. The following is the syntax for initializing the value of a 2D array using nested for loops:
for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { name_of_Array [i][j] = value; } }
Example
Below is an example for declaring a two-dimensional array and initializing its values using different methods in Java:
public class Test { public static void main(String args[]) { // Using direct initialization int[][] int_2DArray = { {1, 2}, {3, 4}, {5,6}}; System.out.println("Using direct initialization"); for (int i = 0; i < 3; i++) { for (int j = 0; j < 2 ; j++) { System.out.print(int_2DArray[i][j] + " "); } System.out.println(); } //using indexing String[][] string_2DArray = new String[2][2]; string_2DArray[0][0] = "Welcome"; string_2DArray[0][1] = "to"; string_2DArray[1][0] = "Tutorials"; string_2DArray[1][1] = "Point"; System.out.println("\nUsing indexing "); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { System.out.print(string_2DArray[i][j] + " "); } } //Using nested for Loops double[][] double_2DArray = new double[2][2]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { double_2DArray[i][j] = (i + 1) * (j + 1) * 1.5; } } System.out.println("\n \nUsing nested for Loops"); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { System.out.print(double_2DArray[i][j] + " "); } System.out.println(); } } }
Output
Using direct initialization 1 2 3 4 5 6 Using indexing Welcome to Tutorials Point Using nested for Loops 1.5 3.0 3.0 6.0
Accessing a 2D Array
A two-dimensional array is represented by two indices, where the first index denotes the position of the array and the second index represents the position of the element within that particular array. For example:
int[][] myArray = {{10, 20, 30}, {11, 21, 31}, {12, 22, 32} }
Below is the image representation of the above two-dimensional array:

Example
Below is an example for accessing an element in a two-dimensional array in Java:
public class Test { public static void main(String[] args) { int[][] myArray = { {10, 20, 30}, {11, 21, 31}, {12, 22, 32} }; int value = myArray[1][2]; System.out.println("The value at myArray[1][2] is: " + value); } }
Output
The value at myArray[1][2] is: 31
Printing a 2D Array
In Java, we can print a two-dimensional array using the following method:
Using Nested For Loops
The most common way to print the elements of a two-dimensional array is using nested for loops, in which the first loop is used to iterate over the rows and the second is used to iterate over the columns. The following is the syntax for printing a 2D array in Java using nested for loops:
for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { System.out.println(name_of_Array [i][j]); } }
Using Arrays.deepToString() Method
The Arrays.deepToString() method is used to print the multidimensional arrays in Java, and it allows us to easily print a two-dimensional array. The following is the syntax for printing a 2D array using the Arrays.deepToString() method:
System.out.println(Arrays.deepToString(name_of_Array))
Example
Below is an example for printing a two-dimensional array using two different methods in Java:
import java.util.Arrays; public class Test { public static void main(String[] args) { //Using nested for loops int[][] myArray_1= { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; System.out.println("Printing a 2D array using nested loops:"); for (int i = 0; i < myArray_1.length; i++) { for (int j = 0; j < myArray_1[i].length; j++) { System.out.print(myArray_1[i][j] + " "); } System.out.println(); } // Using Arrays.deepToString() method String[][] myArray_2 = { {"Welcome", "to", "Tutorials"}, {"Point", "start", "learning"} }; System.out.println("\nPrinting a 2D array using Arrays.deepToString:"); System.out.println(Arrays.deepToString(myArray_2)); } }
Output
Printing a 2D array using nested loops: 1 2 3 4 5 6 7 8 9 Printing a 2D array using Arrays.deepToString: [[Welcome, to, Tutorials], [Point, start, learning]]