Multi-Dimensional Array in Java

Multi-Dimensional Array in Java:

We can use the Eclipse IDE for this example. If you do not know about it then follow this link- How to Install Eclipse For Java and create a program in it.

Multi-Dimensional Arrays represent 2D, 3D, …… arrays that are combinations of several types of arrays. For example, a 2D array is a combination of two or more 1D arrays. Similarly, a 3D array is a combination of two or more 2D arrays. In this blog, we will learn about 2D arrays.

Two-Dimensional Arrays (2D arrays):

A 2D array represents several rows and columns of data. For example, the marks obtained by a group of students in three different subjects can be represented by a 2D array. If we write the marks of three students as:

  • 40, 50, 55
  • 35, 69, 30
  • 40, 70, 80

The preceding marks represent a two-dimensional array since it got three rows (number of students) and in each row three columns (number of subjects). There are totally (3 x 3 = 9) elements. We can take the first row itself as a one-dimensional array. Similarly, the second row is a one-dimensional array and the third row is another one-dimensional array. So the preceding 2D array contains three 1D arrays within it.

Java 2D Array

Source Code:

public class Two_dimensional_array {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int[][] students_marks = new int[3][3];
		
		students_marks[0][0] = 40;
		students_marks[0][1] = 50;
		students_marks[0][2] = 55;
		students_marks[1][0] = 35;
		students_marks[1][1] = 69;
		students_marks[1][2] = 30;
		students_marks[2][0] = 40;
		students_marks[2][1] = 70;
		students_marks[2][2] = 80;
		
		for(int x=0; x<students_marks.length; x++) {
			System.out.println();
			
			for(int y=0; y<students_marks[x].length; y++) {
				System.out.print(students_marks[x][y] + " ");
			}
			
			
		}
		
		

	}

}

Another way to write the above code:

public class Two_dimensional_array {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int[][] students_marks = { {40,50,55},
		                           {35,69,30},
				           {40,70,80} };
		
		students_marks[0][0] = 40;
		students_marks[0][1] = 50;
		students_marks[0][2] = 55;
		students_marks[1][0] = 35;
		students_marks[1][1] = 69;
		students_marks[1][2] = 30;
		students_marks[2][0] = 40;
		students_marks[2][1] = 70;
		students_marks[2][2] = 80;
		
		for(int x=0; x<students_marks.length; x++) {
			System.out.println();
			
			for(int y=0; y<students_marks[x].length; y++) {
				System.out.print(students_marks[x][y] + " ");
			}
			
			
		}
		
		

	}

}

Tutorial MySQL
Tutorial Python
Malicious Software (Malware)
Database Management System (DBMS)
FTP (File Transfer Protocol)
Email (Electronic Mail)
Internet and Its Uses
Java (programming language)– Wikipedia

Comments (No)

Leave a Reply