- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Multidimensional Array
You can simply say multidimensional arrays as arrays of arrays. These, as you might expect, look and act like regular multidimensional arrays. Nevertheless, as you will see that there are a couple of subtle differences.
Java Two Dimensional Array
To declare a multidimensional array variable, determine each additional index using another set of square brackets. For instance, the below code fragment declares a two-dimensional array variable called twoD :
int twoD[][] = new int[4][5];
This allocates a 4 by 5 array and then assigns it to twoD. Actually, this matrix is made as an array of array of int.
Java Two Dimensional Array Example
The following program numbers each element present in the array from left to right, and top to bottom, and then shows these values
/* Java Program Example - Java Multidimensional Arrays * This program demonstrates a two-dimensional (2D) array */ public class JavaProgram { public static void main(String args[]) { int twoD[][] = new int[4][5]; int i, j, k = 0; for(i=0; i<4; i++) { for(j=0; j<5; j++) { twoD[i][j] = k; k++; } } for(i=0; i<4; i++) { for(j=0; j<5; j++) { System.out.print(twoD[i][j] + " "); } System.out.println(); } } }
When the above Java program is compile and executed, it will produce the following output:
When you allocate the memory for a multidimensional array, then you only need to specify the memory for the first (leftmost) dimension. You can also allocate the remaining dimensions separately. For instance, the code given below allocates the memory for the first dimension of twoD when it is declared. It allocates the second dimension manually.
int twoD[][] = new int[4][]; twoD[0] = new int[5]; twoD[1] = new int[5]; twoD[2] = new int[5]; twoD[3] = new int[5];
Although there is no advantage in individually allocating the second dimension arrays in this situation, there may be in others. For instance, when you allocate dimensions manually, you do not need to allocate the same number of elements for each dimension. As told earlier, since multidimensional arrays are arrays of arrays, the length of the each array is under your control. For instance, the given program creates a two-dimensional array in which the sizes of the second dimension are unequal :
/* Java Program Example - Java Two-dimensional Arrays * Manually allocate differing size second dimensions */ public class JavaProgram { public static void main(String args[]) { int twoD[][] = new int[4][]; twoD[0] = new int[1]; twoD[1] = new int[2]; twoD[2] = new int[3]; twoD[3] = new int[4]; int i, j, k = 0; for(i=0; i<4; i++) { for(j=0; j<i+1; j++) { twoD[i][j] = k; k++; } } for(i=0; i<4; i++) { for(j=0; j<i+1; j++) { System.out.print(twoD[i][j] + " "); } System.out.println(); } } }
When the above Java program is compile and executed, it will produce the following output:
In Java, it is possible to initialize multidimensional arrays. To do so, simply enclose each dimension's initializer in its own set of curly braces.
The following program creates a matrix where each element holds the product of row and column indexes. Notice here that you can also use expressions as well as literal values inside of array initializers.
/* Java Program Example - Java Multidimensional Arrays * Initialize a two-dimensional arrays. */ public class JavaProgram { public static void main(String args[]) { double m[][] = { { 0*0, 1*0, 2*0, 3*0 }, { 0*1, 1*1, 2*1, 3*1 }, { 0*2, 1*2, 2*2, 3*2 }, { 0*3, 1*3, 2*3, 3*3 } }; int i, j; for(i=0; i<4; i++) { for(j=0; j<4; j++) { System.out.print(m[i][j] + " "); } System.out.println(); } } }
When the above Java program is compile and executed, it will produce the following output:
As you can see here that each row in the array is initialized as specified in the initialization lists.
Java Three Dimensional Array
Now, let's look this example that uses a multidimensional (three-dimensional) array. The following program creates a 3 by 4 by 5, three-dimensional array. This then loads each element with the product of its indexes. Finally, it displays these products.
Java Three Dimensional Array Example
Here is an example program, demonstrates the concept and use of three dimensional arrays in Java:
/* Java Program Example - Java Multidimensional Arrays * This program demonstrates a three-dimensional array. */ public class JavaProgram { public static void main(String args[]) { /* initializing three-dimensional arrays */ int threeD[][][] = new int[3][4][5]; int i, j, k; for(i=0; i<3; i++) { for(j=0; j<4; j++) { for(k=0; k<5; k++) { threeD[i][j][k] = i * j * k; } } } /* displaying three-dimensional arrays */ for(i=0; i<3; i++) { for(j=0; j<4; j++) { for(k=0; k<5; k++) { System.out.print(threeD[i][j][k] + " "); } System.out.println(); } System.out.println(); } } }
When the above Java program is compile and executed, it will produce the following output:
« Previous Tutorial Next Tutorial »