/**
* 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,
* 例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
* 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
*/
public static void printMatrix() {
int[][] m = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16},
};
ArrayList<Integer> list = new ArrayList<>();
int x = m[0].length;//列 数
int y = m.length;//行 数
int left = 0;//左边界,包含
int top = 0;//上边界,包含
int right = x;//右边界,不包含
int bottom = y;//下边界,不包含
while (left <= right && top <= bottom) {
//打印上边的行,行号是top,范围是[0,bottom-1]
for (int j = left; j < right; j++) {
//行号不变,列号变,列号介于left与right之间
System.out.println("printMatrix--" + m[top][j]);
}
//打印右边面的行,列号是right,范围是[0,right-1]
for (int j = top + 1; j < bottom; j++) {
//列号不变,行号变,由于上面打印了右上角的元素,要排除这个元素,所以top+1
System.out.println("printMatrix--" + m[j][right - 1]);
}
//打印下边的行
for (int j = right - 2; j >= left; j--) {
//行号不变,列号变,由于前面打印了右下角的元素,打印下边元素时,排除这个元素,所以right-1-1
System.out.println("printMatrix--" + m[bottom - 1][j]);
}
//打印左边的行
for (int j = bottom - 2; j > top; j--) {
//列号不变,行号变,由于左上角,左下角都已经打印了,需要排除这两个元素,所以bottom-1-1,j>top
System.out.println("printMatrix--" + m[j][left]);
}
//打印了'第1圈',再打印'第2圈'
//top向下移动一行
top++;
//right向左移动一行
right--;
//bottom向上移动一行
bottom--;
//left向右移动一行
left++;
}
}
12-19 20:03:09.860 4162-4162/com.example.bxh.sayhello I/System.out: printMatrix--1
12-19 20:03:09.860 4162-4162/com.example.bxh.sayhello I/System.out: printMatrix--2
12-19 20:03:09.860 4162-4162/com.example.bxh.sayhello I/System.out: printMatrix--3
12-19 20:03:09.860 4162-4162/com.example.bxh.sayhello I/System.out: printMatrix--4
12-19 20:03:09.860 4162-4162/com.example.bxh.sayhello I/System.out: printMatrix--8
12-19 20:03:09.860 4162-4162/com.example.bxh.sayhello I/System.out: printMatrix--12
12-19 20:03:09.860 4162-4162/com.example.bxh.sayhello I/System.out: printMatrix--16
12-19 20:03:09.860 4162-4162/com.example.bxh.sayhello I/System.out: printMatrix--15
12-19 20:03:09.860 4162-4162/com.example.bxh.sayhello I/System.out: printMatrix--14
12-19 20:03:09.860 4162-4162/com.example.bxh.sayhello I/System.out: printMatrix--13
12-19 20:03:09.860 4162-4162/com.example.bxh.sayhello I/System.out: printMatrix--9
12-19 20:03:09.860 4162-4162/com.example.bxh.sayhello I/System.out: printMatrix--5
12-19 20:03:09.860 4162-4162/com.example.bxh.sayhello I/System.out: printMatrix--6
12-19 20:03:09.860 4162-4162/com.example.bxh.sayhello I/System.out: printMatrix--7
12-19 20:03:09.860 4162-4162/com.example.bxh.sayhello I/System.out: printMatrix--11
12-19 20:03:09.860 4162-4162/com.example.bxh.sayhello I/System.out: printMatrix--10