/**************************************************************************************** * Title: Clockwise Print matrix * Enter a matrix, Print each number in a clockwise order from outward. For example, if you enter the following matrix *1234 *5678 *9101112 *13141516 * Print: 12348121615141395671110 * Time: August 31, 2015 09:02:36 * file: Mergelink.java * Cutter_point ****************************************************************************************/package Bishi. Offer50.y2015.m08.d31;public class Matrixcircle{private int numbers[][];p ublic matrixcircle () {}//constructor public matrixcircle (int nums[][]) {numbers = Nums;} /** * Print a lap * @param start */private void printcircle (int start) {int rows = numbers.length;int columns = Numbers.clone (). Le Ngth;int endColumn = columns-1-Start;int endrow = rows-1-start;//from left to right (int i = start; I <= endColumn; ++i) {in T number = Numbers[start][i]; System.out.print (number + "\ T");} for//from top to bottom if (Endrow > Start) {for (int i = start + 1; I <= endrow; ++i) {int number = Numbers[i][endcolumn]; System.out.print (number + "\ T");} for}//if//right-to-left if (Start < enDcolumn && Start < Endrow) {for (int i = endColumn-1; I >= start; i.) {int number = Numbers[endrow][i]; System.out.print (number + "\ T");} for}//if//from bottom to Upper if (Start < endRow-1 && Start < EndColumn) {for (int i = endRow-1; i > start; i) {int n Umber = Numbers[i][start]; System.out.print (number + "\ T");} For}//ifsystem.out.println ();} /** * input US matrix */public void printmaxtrixclockwisely () {if (numbers = = null) return;int start = 0;int rows = Numbers.length;int columns = Numbers.clone (). length;//we terminate the condition that start is in the middle position while (start * 2 < rows && Start * 2 < columns) {this. Printcircle (start); ++start;} While}}
When I discovered the JUnit test, I asked the constructor of this class to be a public and a parameterless class, so we have parameters in this constructor, so we have to write a test class again.
Package Bishi. Offer50.y2015.m08.d31;public class test{@org. junit.testpublic void Test () {int numbers[][] = {{1,2,3,4},{5,6,7,8},{ 9,10,11,12},{13,14,15,16}}; matrixcircle mc = new Matrixcircle (numbers); mc.printmaxtrixclockwisely ();}}
OutPut:
1 2 3 4 8 (9 ) 5
6 7
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Written test" 37, clockwise print matrix