编程如下:
欢迎大家前来讨论~
public class PrintMatirx {
 public static void main(String args[]) {
 int arr[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
 printZigMatrix(arr);
 }
 public static void printZigMatrix(int arr[][]) {
 int row1 = 0;
 int col1 = 0;
 int row2 = 0;
 int col2 = 0;
 int helpR = arr.length - 1;
 int helpC = arr[0].length - 1;
 boolean dir = false;
 /* 下面是来判断(row1,col1)点是往右方移动还是往下边移动,当col1 == helpC时,就要往下面移动了,下面同理 */
 while (row1 <= helpR) {// 什么时候停止对角线遍历呢?当row > helpR时,说明已经遍历到底部了,就可以停止遍历了
 printDiagonal(arr, row1, col1, row2, col2, dir);
 if (col1 == helpC) {
 row1++;
 } else {
 col1++;
 }
 if (row2 == helpR) {
 col2++;
 } else {
 row2++;
 }
 dir = !dir;// 方向每次都调转一次
 }
 }
 /* 按照对角线方向打印出矩阵的元素,因为是之字形,所以要定义一个标志dir来使其一次往右上方打印,一次往左下方打印 */
 public static void printDiagonal(int arr[][], int row1, int col1, int row2, int col2, boolean dir) {
 if (dir) {
 while (row1 <= row2) {// 打印停止的判断:row1 <= row2的时候,可以继续往下打印,当row1 > row2时,停止打印,下面也是同理
 System.out.print(arr[row1++][col1--] + " ");// 怎么做到往对角线打印,可以通过row++同时col--来实现
 }
 } else {
 while (row2 >= row1) {
 System.out.print(arr[row2--][col2++] + " ");
 }
 }
 }
}