import java.util.ArrayList; public class Solution { public ArrayList printMatrix(int [][] matrix) { int row = matrix.length - 1; int col = matrix[0].length - 1; System.out.println(row); System.out.println(col); int i = 0; int j = 0; ArrayList list = new ArrayList<>(); while(i <= row && j <= col){
for(int temp = i;temp <= col;temp ++){
list.add(matrix[i][temp]);
}
if(++ i > row) break;
for(int temp = i;temp <= row;temp ++){
list.add(matrix[temp][col]);
}
if(-- col < j) break;
for(int temp = col ;temp >= j;temp --){
list.add(matrix[row][temp]);
}
if(-- row < i)break;
for(int temp = row ;temp >= i;temp --){
list.add(matrix[temp][j]);
}
if(++ j > col) break;
}
return list;
}
}