Two Dimensional Array in Java


Two Dimensional array can be defined as a multidimensional array in Java program. Two Dimensional Array represented as the collection of rows and columns.


Declaration of Two Dimensional Array:

To declare the 2D array in Java programming language.

Syntax:-

 Data_type[][] Array_name;

Example:-

int[][] a=new int[4][5]; 

Here, a is Two-dimensional array, this array can hold 6 elements.

Java 2D Array


Initialization of Two Dimensional Array:

Multidimensional arrays initialized by specifying bracketed values for every row.

 int[][] score= {{100, 221}, {59, 54},{10, 21}};

Example:-

 public class Twodarray 
{  
    public static void main(String []args) {      
		int[][] score = {{100, 221}, {59, 54},{10, 21}};  
        for(int i=0; i < 3; i++){ 
        	for(int j=0; j < 2; j++){     
                System.out.println(	" Element[" + i + "][" + j + "]= " + score[i][j]); 
            }    
        }
    }
} 



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.