
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print 2D Array or Matrix in Java Programming
In this post we will try to print an array or matrix of numbers at console in same manner as we generally write on paper.
For this the logic is to access each element of array one by one and make them print separated by a space and when row get to emd in matrix then we will also change the row
Example
public class Print2DArray { public static void main(String[] args) { final int[][] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; for (int i = 0; i < matrix.length; i++) { //this equals to the row in our matrix. for (int j = 0; j < matrix[i].length; j++) { //this equals to the column in each row. System.out.print(matrix[i][j] + " "); } System.out.println(); //change line on console as row comes to end in the matrix. } } }
Output
1 2 3 4 5 6 7 8 9
Advertisements