我是靠谱客的博主 从容柜子,这篇文章主要介绍Java 数组转置 方阵顺时针旋转90度,现在分享给大家,希望可以做个参考。

一维数组转置(首尾交换)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class array { public static void main(String args[]){ int data [] =new int [] {1,2,3,4,5,6}; exchange(data); print1(data); } //首尾交换 public static void exchange(int temp[]){ int head=0; int tail=temp.length-1; for(int i=0;i<temp.length/2;i++){ int k=temp[head]; temp[head]=temp[tail]; temp[tail]=k; head++; tail--; } } //输出 public static void print1(int temp[]){ for(int s=0;s<temp.length;s++){ System.out.println(temp[s]); } } }

二维数组转置


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class helloword { public static void main(String args[]){ int data [][] = new int [][] {{1,2,3},{4,5,6},{7,8,9}}; reverse(data); print1(data); } //将矩阵转置 public static void reverse(int temp [][] ){ for(int i=0;i<temp.length;i++){ for(int j=i;j<temp[i].length;j++){ int k=temp[i][j]; temp[i][j]=temp[j][i]; temp[j][i]=k; } } } //将矩阵输出 public static void print1(int temp[][]){ for(int i=0;i<temp.length;i++){ for(int j=0;j<temp[i].length;j++){ System.out.print(temp[i][j]+"t"); } System.out.println(); } } }

 

方阵顺时针旋转90


方法一:可以看出,将二维数组顺时针旋转90度,就是将其转置后的数组的列进行前后交换(即第一列变为最后一列,第二列变为倒数第二列)

方法二:通过观察,可以看出:

  1.  列号变为行号
  2. n-行号)变为列号(其中n为方阵维数减1



 

若要将矩阵顺时针旋转180,两次调用旋转90度的函数就可以。

方法一、方法二不仅针对三维方阵,其他维数的方阵也适合。

 

 

 

方法一:


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class helloword { public static void main(String args[]){ int data [][] = new int [][] {{1,2,3},{4,5,6},{7,8,9}}; reverse(data); exchange(data); print1(data); } //将矩阵转置 public static void reverse(int temp [][] ){ for(int i=0;i<temp.length;i++){ for(int j=i;j<temp[i].length;j++){ int k=temp[i][j]; temp[i][j]=temp[j][i]; temp[j][i]=k; } } } //将转置后的矩阵的列交换(第一列跟最后一列交换,第二列跟倒数第二列交换) public static void exchange(int temp[][]){ int a=0; int b=temp.length-1; for(int i=0;i < (temp.length)/2;i++){ for(int j=0;j<temp.length;j++){ int k=temp[j][a]; temp[j][a]=temp[j][b]; temp[j][b]=k; } a++; b--; } } //将矩阵输出 public static void print1(int temp[][]){ for(int i=0;i<temp.length;i++){ for(int j=0;j<temp[i].length;j++){ System.out.print(temp[i][j]+"t"); } System.out.println(); } } }

方法二:


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class arrayEx { public static void main(String args[]){ int data [][] =new int [][] {{1,2,3},{4,5,6},{7,8,9}}; rotate(data); print1(data); } //旋转90度 public static void rotate(int temp[][]){ int len=temp.length; int b [][]=new int [len][len]; for(int i=0;i<len;i++){ for(int j=0;j<len;j++){ b[j][len-1-i]=temp[i][j]; } } for(int i=0;i<len;i++) for(int j=0;j<len;j++) temp[i][j]=b[i][j]; } //输出 public static void print1(int temp[][]){ for(int i=0;i<temp.length;i++){ for(int j=0;j<temp[i].length;j++){ System.out.print(temp[i][j]+"t"); } System.out.println(); } } }


最后

以上就是从容柜子最近收集整理的关于Java 数组转置 方阵顺时针旋转90度的全部内容,更多相关Java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(104)

评论列表共有 0 条评论

立即
投稿
返回
顶部