ในการพิมพ์เมทริกซ์ในรูปแบบ Z โค้ด Java มีดังต่อไปนี้ -
ตัวอย่าง
import java.lang.*;
import java.io.*;
public class Demo{
public static void z_shape(int my_arr[][], int n){
int i = 0, j, k;
for (j = 0; j < n - 1; j++){
System.out.print(my_arr[i][j] + " ");
}
k = 1;
for (i = 0; i < n - 1; i++){
for (j = 0; j < n; j++){
if (j == n - k){
System.out.print(my_arr[i][j] + " ");
break;
}
}
k++;
}
i = n - 1;
for (j = 0; j < n; j++)
System.out.print(my_arr[i][j] + " ");
System.out.print("\n");
}
public static void main(String[] args){
int my_arr[][] = { { 34, 67, 89, 0},{ 0, 1,0, 1 },{ 56, 99, 102, 21 },{78, 61, 40, 99}};
System.out.println("The matrix is ");
z_shape(my_arr, 4);
}
} ผลลัพธ์
The matrix is 34 67 89 0 0 99 78 61 40 99
คลาสชื่อ Demo กำหนดฟังก์ชันชื่อ 'z_shape' ซึ่งวนซ้ำผ่านอาร์เรย์โดยทำตามรูปร่างของ 'z' ในฟังก์ชันหลัก อาร์เรย์หลายมิติถูกกำหนด และฟังก์ชันถูกเรียกใช้โดยการส่งผ่านอาร์เรย์นี้ เอาต์พุตที่เกี่ยวข้องจะแสดงบนคอนโซล