Two Dimensional Array S. KAVITHA ASSISTANT PROFESSOR SRI SARADA NIKETAN COLLEGE OF SCIENCE FOR WOMEN KARUR-05
• In Java Two Dimensional Array, data stored in row and columns, and we can access the record using both the row index and column index
Two Dimensional Array Declaration in Java • The following code snippet shows the two dimensional array declaration in Java Programming Language: Data_Type[][] Array_Name;
• Data_type: It decides the type of elements it will accept. For example, If we want to store integer values, then the Data Type will be declared as int. If we want to store Float values, then the Data Type will be float. • Array_Name: This is the name to give it to this Java two dimensional array. For example, Car, students, age, marks, department, employees, etc.
Create Two dimensional Array in Java In order to create a two dimensional array in Java, we have to use the New operator as we shown below: • Data_Type[][] Array_Name = new int[Row_Size] [Column_Size]; If we observe the above two dimensional array code snippet, • Row_Size: Number of Row elements an array can store. For example, Row_Size = 5, then the array will have five rows. • Column_Size: Number of Column elements an array can store. For example, Column_Size = 6, then the array will have 6 Columns.
double [][] anStudentArray; // Declaration of Two dimensional array in java // Crating an Java two dimensional Array anStudentArray = new int[5][3]; For Example, double [][] Employees = new double[5][3];
Two Dimensional Array First Approach Declaring and Creating a Two Dimensional Array in Java • int[][] Student_Marks = new int[2][3]; Initialize Array elements more traditionally. Student_Marks[0][0] = 15; // Initializing Array elements at position [0][0] Student_Marks[1] [1] = 45; // Initializing Array elements at position [1][1] Student_Marks[2][1] = 65; // Initializing Array elements at position [2][1]
• // Two Dimensional Array in Java Example package ArrayDefinitions; public class TwoDimentionalArray { public static void main(String[] args) { int[][] a = { {15, 25, 35}, {45, 55, 65} }; int[][] b = {{12, 22, 32}, {55, 25, 85} }; int[][] Sum = new int[2][3]; int rows, columns; for(rows = 0; rows < a.length; rows++) { for(columns = 0; columns < a[0].length; columns++) { Sum[rows][columns] = a[rows][columns] + b[rows][columns]; } } System.out.println("Sum Of those Two Arrays are: "); for(rows = 0; rows < a.length; rows++) { for(columns = 0; columns < a[0].length; columns++) { System.out.format("%d t", Sum[rows][columns]); } System.out.println(""); } } }

How to create a two-dimensional array in java

  • 1.
    Two Dimensional Array S.KAVITHA ASSISTANT PROFESSOR SRI SARADA NIKETAN COLLEGE OF SCIENCE FOR WOMEN KARUR-05
  • 2.
    • In JavaTwo Dimensional Array, data stored in row and columns, and we can access the record using both the row index and column index
  • 3.
    Two Dimensional ArrayDeclaration in Java • The following code snippet shows the two dimensional array declaration in Java Programming Language: Data_Type[][] Array_Name;
  • 4.
    • Data_type: Itdecides the type of elements it will accept. For example, If we want to store integer values, then the Data Type will be declared as int. If we want to store Float values, then the Data Type will be float. • Array_Name: This is the name to give it to this Java two dimensional array. For example, Car, students, age, marks, department, employees, etc.
  • 5.
    Create Two dimensionalArray in Java In order to create a two dimensional array in Java, we have to use the New operator as we shown below: • Data_Type[][] Array_Name = new int[Row_Size] [Column_Size]; If we observe the above two dimensional array code snippet, • Row_Size: Number of Row elements an array can store. For example, Row_Size = 5, then the array will have five rows. • Column_Size: Number of Column elements an array can store. For example, Column_Size = 6, then the array will have 6 Columns.
  • 6.
    double [][] anStudentArray; //Declaration of Two dimensional array in java // Crating an Java two dimensional Array anStudentArray = new int[5][3]; For Example, double [][] Employees = new double[5][3];
  • 7.
    Two Dimensional ArrayFirst Approach Declaring and Creating a Two Dimensional Array in Java • int[][] Student_Marks = new int[2][3]; Initialize Array elements more traditionally. Student_Marks[0][0] = 15; // Initializing Array elements at position [0][0] Student_Marks[1] [1] = 45; // Initializing Array elements at position [1][1] Student_Marks[2][1] = 65; // Initializing Array elements at position [2][1]
  • 8.
    • // TwoDimensional Array in Java Example package ArrayDefinitions; public class TwoDimentionalArray { public static void main(String[] args) { int[][] a = { {15, 25, 35}, {45, 55, 65} }; int[][] b = {{12, 22, 32}, {55, 25, 85} }; int[][] Sum = new int[2][3]; int rows, columns; for(rows = 0; rows < a.length; rows++) { for(columns = 0; columns < a[0].length; columns++) { Sum[rows][columns] = a[rows][columns] + b[rows][columns]; } } System.out.println("Sum Of those Two Arrays are: "); for(rows = 0; rows < a.length; rows++) { for(columns = 0; columns < a[0].length; columns++) { System.out.format("%d t", Sum[rows][columns]); } System.out.println(""); } } }