package exercise;
/*
* 二维数组的转置
* 转置的前提是数组的行和列必须相等。
*/
public class Demo1 {
public static void main(String[] args) {
int data[][] = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
reverse(data);
print(data);
}
public static void reverse(int temp[][]){//翻转二维数组
for(int x = 0;x<temp.length;x++){
for(int y= x;y<temp[x].length;y++){
int t = temp[x][y];
temp[x][y]= temp[y][x];
temp[y][x] = t;
}
}
}
public static void print(int temp[][]){
for(int x = 0;x<temp.length;x++){
for(int y = 0;y<temp[x].length;y++){
System.out.print(temp[x][y]+"、");
}
System.out.println();
}
System.out.println();
}
}
/*
* 二维数组的转置
* 转置的前提是数组的行和列必须相等。
*/
public class Demo1 {
public static void main(String[] args) {
int data[][] = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
reverse(data);
print(data);
}
public static void reverse(int temp[][]){//翻转二维数组
for(int x = 0;x<temp.length;x++){
for(int y= x;y<temp[x].length;y++){
int t = temp[x][y];
temp[x][y]= temp[y][x];
temp[y][x] = t;
}
}
}
public static void print(int temp[][]){
for(int x = 0;x<temp.length;x++){
for(int y = 0;y<temp[x].length;y++){
System.out.print(temp[x][y]+"、");
}
System.out.println();
}
System.out.println();
}
}
本文介绍了一种在Java中实现二维数组转置的方法。通过一个示例程序展示了如何交换数组的行和列来完成转置操作,适用于方阵类型的二维数组。
1万+

被折叠的 条评论
为什么被折叠?



