Java如何实现矩阵顺时针逆时针旋转打印示例?
- 内容介绍
- 文章标签
- 相关推荐
本文共计672个文字,预计阅读时间需要3分钟。
javapublic class SnakeMatrix { // 定义矩阵的阶数 private int n; // 填充矩阵的值 private int k=1; // 矩阵数据 private int[][] data;
public SnakeMatrix(int n) { this.n=n; this.data=new int[n][n]; fillMatrix(); }
private void fillMatrix() { int i=0, j=0; boolean leftToRight=true;
for (int num=1; num <=n * n; num++) { data[i][j]=num; if (leftToRight) { if (j==n - 1 || i==n - 1) { leftToRight=false; if (i==n - 1) { j--; } else { i++; } } else { j++; } } else { if (i==0 || j==0) { leftToRight=true; if (j==0) { i++; } else { j--; } } else { i--; } } } }
public void printMatrix() { for (int i=0; i public static void main(String[] args) { SnakeMatrix matrix=new SnakeMatrix(4); matrix.printMatrix(); }} java实现的顺时针/逆时针打印矩阵操作。分享给大家供大家参考,具体如下:
public class SnakeMatrix {
/**
* 定义矩阵的阶数
*/
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() {
//定义行数
int rowLen = data.length;
//定义列数
int columnLen = data.length;
//移动方向
Direction direction = Direction.right;
//定义上边界
int upBound = 0;
//定义下边界
int downBound = rowLen - 1;
//定义左边界
int leftBound = 0;
//定义右边界
int rightBound = columnLen - 1;
//矩阵当前行数
int row = 0;
//矩阵当前列数
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(); } }
直接进行使用,有两个方法,一个正序一个倒序
更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
本文共计672个文字,预计阅读时间需要3分钟。
javapublic class SnakeMatrix { // 定义矩阵的阶数 private int n; // 填充矩阵的值 private int k=1; // 矩阵数据 private int[][] data;
public SnakeMatrix(int n) { this.n=n; this.data=new int[n][n]; fillMatrix(); }
private void fillMatrix() { int i=0, j=0; boolean leftToRight=true;
for (int num=1; num <=n * n; num++) { data[i][j]=num; if (leftToRight) { if (j==n - 1 || i==n - 1) { leftToRight=false; if (i==n - 1) { j--; } else { i++; } } else { j++; } } else { if (i==0 || j==0) { leftToRight=true; if (j==0) { i++; } else { j--; } } else { i--; } } } }
public void printMatrix() { for (int i=0; i public static void main(String[] args) { SnakeMatrix matrix=new SnakeMatrix(4); matrix.printMatrix(); }} java实现的顺时针/逆时针打印矩阵操作。分享给大家供大家参考,具体如下:
public class SnakeMatrix {
/**
* 定义矩阵的阶数
*/
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() {
//定义行数
int rowLen = data.length;
//定义列数
int columnLen = data.length;
//移动方向
Direction direction = Direction.right;
//定义上边界
int upBound = 0;
//定义下边界
int downBound = rowLen - 1;
//定义左边界
int leftBound = 0;
//定义右边界
int rightBound = columnLen - 1;
//矩阵当前行数
int row = 0;
//矩阵当前列数
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(); } }
直接进行使用,有两个方法,一个正序一个倒序
更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。

