Experts Of Programming Created By: Anonymous
What is 2-D Array? • Array of Arrays. • 2-D array usually represented with rows and columns
2-D Array Declaration • We can declare 2-D array in following ways type[][] referencevariable; or type referencevariable[][]; For example: int marks[][]; Each element in the 2-D Array must be the same type.
Creation of 2-D Array arrayreferencevariable = new type[value][value]; For example : marks = new int[3][4];
More on 2-D Array Creation marks = new int[][] // incorrect; marks= new int[][4] // incorrect; marks = new int[3][] // correct;
2-D array in form of rows and columns: marks=new int[3][4];
Initialization of 2-D Array • After array creation we can initialize the 2-D array like this : • Arrayvariable[row][column]= value; • For example lets initialize the marks array :
marks[0][0]=8; marks[0][1]=9; marks[0][2]=10; marks[0][3]=7; 8 9 10 7Row 0 Col 0 Col 1 Col 3 Col 4 12 21 30 7 31 45 10 40 Row 1 Row 2 Col 0 Col 1 Col 2 Col 3 Col 0 Col 1 Col 2 Col 3 marks[1][0]=12; marks[1][1]=21; marks[1][2]=30; marks[1][3]=7; marks[2][0]=31; marks[2][1]=45; marks[2][2]=10; marks[2][3]=40;
8 9 10 7 12 21 30 7 31 45 10 40 Col 0 Col 2 Col 3Col 1 Row 0 Row 1 Row 2 Now if you want to access any element from 2-D array System.Out.Println(marks[1][2]); output = 30 System.Out.Println(marks[0][0]); output = 8
We can also initialize the array at the time of array creation int[][] marks = new int{ {1,2,0,5}, {4,5,1,4}, {7,6,5,9} }
How we can get array length marks.length // tell us rows. marks[0].length // tell us columns.
THANKYOU

Two-dimensional array in java

  • 1.
  • 2.
    What is 2-DArray? • Array of Arrays. • 2-D array usually represented with rows and columns
  • 3.
    2-D Array Declaration •We can declare 2-D array in following ways type[][] referencevariable; or type referencevariable[][]; For example: int marks[][]; Each element in the 2-D Array must be the same type.
  • 4.
    Creation of 2-DArray arrayreferencevariable = new type[value][value]; For example : marks = new int[3][4];
  • 5.
    More on 2-DArray Creation marks = new int[][] // incorrect; marks= new int[][4] // incorrect; marks = new int[3][] // correct;
  • 6.
    2-D array inform of rows and columns: marks=new int[3][4];
  • 7.
    Initialization of 2-DArray • After array creation we can initialize the 2-D array like this : • Arrayvariable[row][column]= value; • For example lets initialize the marks array :
  • 8.
    marks[0][0]=8; marks[0][1]=9; marks[0][2]=10;marks[0][3]=7; 8 9 10 7Row 0 Col 0 Col 1 Col 3 Col 4 12 21 30 7 31 45 10 40 Row 1 Row 2 Col 0 Col 1 Col 2 Col 3 Col 0 Col 1 Col 2 Col 3 marks[1][0]=12; marks[1][1]=21; marks[1][2]=30; marks[1][3]=7; marks[2][0]=31; marks[2][1]=45; marks[2][2]=10; marks[2][3]=40;
  • 9.
    8 9 107 12 21 30 7 31 45 10 40 Col 0 Col 2 Col 3Col 1 Row 0 Row 1 Row 2 Now if you want to access any element from 2-D array System.Out.Println(marks[1][2]); output = 30 System.Out.Println(marks[0][0]); output = 8
  • 10.
    We can alsoinitialize the array at the time of array creation int[][] marks = new int{ {1,2,0,5}, {4,5,1,4}, {7,6,5,9} }
  • 11.
    How we canget array length marks.length // tell us rows. marks[0].length // tell us columns.
  • 12.