java實現(xiàn)的順時針/逆時針打印矩陣操作示例
java實現(xiàn)的順時針/逆時針打印矩陣操作。分享給大家供大家參考,具體如下:
public class SnakeMatrix {
/**
* 定義矩陣的階數(shù)
*/
private int n;
//填充矩陣的值
private int k = 1;
private int[][] data;
/**
* 定義矩陣移動的方向
*/
public enum Direction {
left, right, up, down,
}
SnakeMatrix(int n) {
this.n = n;
data = new int[n][n];
}
public void clockwisePrintMatrix() {
//定義行數(shù)
int rowLen = data.length;
//定義列數(shù)
int columnLen = data.length;
//移動方向
Direction direction = Direction.right;
//定義上邊界
int upBound = 0;
//定義下邊界
int downBound = rowLen - 1;
//定義左邊界
int leftBound = 0;
//定義右邊界
int rightBound = columnLen - 1;
//矩陣當(dāng)前行數(shù)
int row = 0;
//矩陣當(dāng)前列數(shù)
int column = 0;
while (true) {
data[row][column] = k++;
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; i++) {
for (int j = 0; j < n; j++) {
System.out.printf("%2d%s", data[i][j], " ");
}
System.out.println();
}
}
}
首先呢上面是定義一個工具類,
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();
}
}
直接進行使用,有兩個方法,一個正序一個倒序
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
Java學(xué)習(xí)之反射機制及應(yīng)用場景介紹
本篇文章主要介紹了Java反射機制及應(yīng)用場景,反射機制是很多Java框架的基石。非常具有實用價值,需要的朋友可以參考下。2016-11-11
SpringBoot中使用@ControllerAdvice注解詳解
這篇文章主要介紹了SpringBoot中使用@ControllerAdvice注解詳解,@ControllerAdvice,是Spring3.2提供的新注解,它是一個Controller增強器,可對controller中被 @RequestMapping注解的方法加一些邏輯處理,需要的朋友可以參考下2023-10-10
Nacos與SpringBoot實現(xiàn)配置管理的開發(fā)實踐
在微服務(wù)架構(gòu)中,配置管理是一個核心組件,而Nacos為此提供了一個強大的解決方案,本文主要介紹了Nacos與SpringBoot實現(xiàn)配置管理的開發(fā)實踐,具有一定的參考價值2023-08-08
Spring Boot啟動時調(diào)用自己的非web邏輯
在spring Boot中,有些代碼是WEB功能,例如API等,但是有些邏輯是非WEB,啟動時就要調(diào)用并持續(xù)運行的,該如何加載自己的非WEB邏輯呢,下面通過實例代碼給大家講解,一起看看吧2017-07-07

