Boundary Elements of Matrix

This program prints boundary elements of a matrix.

Problem Statement

Write a Java program to print boundary elements of a 4x4 matrix.

Source Code

1 public class MatrixBoundaryElements {
2 public static void main(String[] args) {
3 System.out.println("Eduinq Matrix Boundary Elements");
4 int[][] A = {
5 {1,2,3,4},
6 {5,6,7,8},
7 {9,10,11,12},
8 {13,14,15,16}
9 };
10 for(int i=0;i<4;i++){
11 for(int j=0;j<4;j++){
12 if(i==0||i==3||j==0||j==3)
13 System.out.print(A[i][j]+" ");
14 else System.out.print(" ");
15 }
16 System.out.println();
17 }
18 }
19 }

Program Output

Eduinq Matrix Boundary Elements
1 2 3 4 
5     8 
9     12 
13 14 15 16 

Explanation

Boundary elements are those in the first/last row or first/last column, so the condition checks i==0, i==n-1, j==0, j==n-1 and prints spaces elsewhere, demonstrating how multidimensional arrays can isolate boundary values.

Quick Links to Explore