写程序,在main方法中定义一个大小为4*4的二维字符型数组,数组元素如下所示:
a b c d
e f g h
i j k l
m n o p
请分别输出数组正反对角线上的元素值。
写程序,在main方法中定义一个大小为4*4的二维字符型数组,数组元素如下所示:
a b c d
e f g h
i j k l
m n o p
请分别输出数组正反对角线上的元素值。
这样么
public static void main(String[] args) {
char[][] array = {{'a', 'b', 'c', 'd'},
{'e', 'f', 'g', 'h'},
{'i', 'j', 'k', 'l'},
{'m', 'n', 'o', 'p'}};
System.out.print("正对角线上的元素为:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i][i] + " ");
}
System.out.println();
System.out.print("反对角线上的元素为:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i][array.length - 1 - i] + " ");
}
}