Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to print Matrix clockwise / counterclockwise with java

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you java how to achieve clockwise / counterclockwise printing matrix operation, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Clockwise / counterclockwise printing matrix operation realized by java. The details are as follows:

Public class SnakeMatrix {/ * defines the order of the matrix * / private int n; / / the value of the filling matrix private int k = 1; private int [] [] data; / * defines the direction of matrix movement * / public enum Direction {left, right, up, down,} SnakeMatrix (int n) {this.n = n; data = new int [n] [n] } public void clockwisePrintMatrix () {/ / define the number of rows int rowLen = data.length; / / define the number of columns int columnLen = data.length; / / move direction Direction direction = Direction.right; / / define the upper boundary int upBound = 0; / define the lower boundary int downBound = rowLen-1; / / define the left boundary int leftBound = 0; / / define the right boundary int rightBound = columnLen-1 / / current rows of matrix int row = 0; / current number of columns of matrix int column = 0; while (true) {data [row] [column] = kryptonite; if (upBound = = downBound & & leftBound = = rightBound) {/ / System.out.println ("upBound:" + upBound + "downBound:" + downBound+ "leftBound:" + leftBound + "rightBound:" + rightBound); break } switch (direction) {case right: if (column

< rightBound) { ++column; } else { ++row; direction = Direction.down; ++upBound; } break; case down: if (row < downBound) { ++row; } else { --column; direction = Direction.left; --rightBound; } break; case up: if (row >

UpBound) {--row;} else {+ + column; direction = Direction.right; + + leftBound;} break; case left: if (column > leftBound) {--column;} else {- row; direction = Direction.up -- downBound;} break; default: break;}} for (int I = 0; I

< n; i++) { for (int j = 0; j < n; j++) { System.out.printf("%2d%s", data[i][j], " "); } System.out.println(); } } public void anticlockwisePrintMatrix() { int rowLen = data.length; int columnLen = data.length; int leftBound = 0; int rightBound = columnLen - 1; int upBound = 0; int downBound = rowLen - 1; int row = 0; int column = 0; Direction direction = Direction.down; while (true) { data[row][column] = k++; if (rightBound == leftBound && upBound == downBound) { break; } switch (direction) { case down: if (row < downBound) { row++; } else { column++; direction = Direction.right; leftBound++; } break; case right: if (column < rightBound) { column++; } else { row--; direction = Direction.up; downBound--; } break; case up: if (row >

UpBound) {row--;} else {direction = Direction.left; column--; rightBound--;} break; case left: if (column > leftBound) {column--;} else {direction = Direction.down; row++ UpBound++;} break; default: break;} for (int I = 0; I < n; iTunes +) {for (int j = 0; j < n; jacks +) {System.out.printf ("% 2d%s", data [I] [j], ");} System.out.println ();}

First of all, the above is to define a tool class

Public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); int number = 5; SnakeMatrix snakeMatrix = new SnakeMatrix (number); snakeMatrix.anticlockwisePrintMatrix (); / / snakeMatrix.clockwisePrintMatrix ();}}

To use it directly, there are two methods, one in positive order and one in reverse order

These are all the contents of the article "how to print matrix clockwise / counterclockwise by java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report