Java一维数组和二维数组的转置

数组的转置就是将数组的内容倒序进行保存。例如:数组从下标0开始元素内容为1,2,3,4,5,6,将这个数组转置以后从下标0开始元素内容为6,5,4,3,2,1。

一维数组的转置

思路一、定义一个新的数组,将原始数组按照倒序的方式插入新的数组中,随后改变原始数组的引用。
public class ArraysDemo {
    public static void main(String[] args) {
        int data[] = new int[]{90, 52, 3, 56, 66, 32, 12, 0, 26};
        //定义一个新数组
        int temp[] = new int[data.length];
        int foot = data.length-1;//控制data数组的索引
        //对于新的数组按照索引由小到大的顺序循环
        for (int i = 0; i < temp.length; i++) {
            temp[i] = data[foot--];
        }
        //让data指向temp,而data的原始数据就成了堆内存中的垃圾值
        data = temp;
        show(data);
    }
 
    public static void show(int temp[]) {
        for (int i = 0; i < temp.length; i++) {
            System.out.print(temp[i] + "\t");
        }
    }
}
复制代码

虽然以上代码实现转置,但是在对堆内存中留下的data原数据,垃圾值。

思路二、利用算法,在一个数组上完成转置操作

原始数组:90, 52, 3, 56, 66, 32, 12, 0, 26

第一次转置(将第一个和最后一个交换位置): 26, 52, 3, 56, 66, 32, 12, 0, 90

第二次转置(将第二个和倒数第二个进行转置) 26, 0, 3, 56, 66, 32, 12, 52, 90

第三次转置: 26, 0, 12, 56, 66, 32, 3, 52, 90

第四次转置: 26, 0, 12, 32,66, 56, 3, 52, 90

转换次数为:数组的长度/2(不管是奇数个数还是偶数的个数,转置次数计算方式一样。Int类型不保留小数位,所以结果一样)

实现代码:
public class ArraysDemo {
    public static void main(String[] args) {
        int data[] = new int[]{90, 52, 3, 56, 66, 32, 12, 0, 26};
        reverse(data);
        show(data);
    }
 
    public static void show(int temp[]) {
        for (int i = 0; i < temp.length; i++) {
            System.out.print(temp[i] + "\t");
        }
    }
 
    //数组转置操作方法
    public static void reverse(int temp[]) {
        //转置的次数
        int sum = temp.length / 2;
        //开始的索引
        int start = 0;
        //结尾的索引
        int end = temp.length - 1;
        for (int i = 0; i < sum; i++) {
            int num = temp[start];
            temp[start] = temp[end];
            temp[end] = num;
            start++;
            end--;
        }
    }
}
复制代码

等行等列的二维数组实现转置

分析:

前提:行与列长度相同的二维数组

1[x=0][y=0] 2[x=0][y=1] 3[x=0][y=2]

4[x=1][y=0] 5[x=1][y=1] 6[x=1][y=2]

7[x=2][y=0] 8[x=2][y=1] 9[x=2][y=2]

第一次转换(x=0,y=x=0 循环3次):

Y的第一次循环(x==y) 不满足 x!=y 不进行交换

1[x=0][y=0] 2[x=0][y=1] 3[x=0][y=2]

4[x=1][y=0] 5[x=1][y=1] 6[x=1][y=2]

7[x=2][y=0] 8[x=2][y=1] 9[x=2][y=2]

Y的第二次循环 (x=0 y=1,判断条件满足,进行交换)

1[x=0][y=0] 4[x=1][y=0] 3[x=0][y=2]

2[x=0][y=1] 5[x=1][y=1] 6[x=1][y=2]

7[x=2][y=0] 8[x=2][y=1] 9[x=2][y=2]

Y的第三次循环 (x=0 y=2,判断条件满足,进行交换)

1[x=0][y=0] 4[x=1][y=0] 7[x=2][y=0]

2[x=0][y=1] 5[x=1][y=1] 6[x=1][y=2]

3[x=0][y=2] 8[x=2][y=1] 9[x=2][y=2]

第二次转换 (x=1,y=x=1 循环2次)

Y的第一次循环(x=1 y=1 条件不满足 不进行交换)

1[x=0][y=0] 4[x=1][y=0] 7[x=2][y=0]

2[x=0][y=1] 5[x=1][y=1] 6[x=1][y=2]

3[x=0][y=2] 8[x=2][y=1] 9[x=2][y=2]

Y的第二次循环(x=1 y=2 进行交换)

1[x=0][y=0] 4[x=1][y=0] 7[x=2][y=0]

2[x=0][y=1] 5[x=1][y=1] 8[x=2][y=1]

3[x=0][y=2] 6[x=1][y=2] 9[x=2][y=2]

第三次转换(x=2 y=x=2 循环一次)

Y的第一次循环(x=2,y=2,不交换)

1[x=0][y=0] 4[x=1][y=0] 7[x=2][y=0]

2[x=0][y=1] 5[x=1][y=1] 8[x=2][y=1]

3[x=0][y=2] 6[x=1][y=2] 9[x=2][y=2]

实现代码:
public class ArraysDemo {
   public static void main(String[] args) {
       int data[][] = new int[][]{
               {1, 2, 3},
               {4, 5, 6},
               {7, 8, 9}
       };
       reverse(data);
       show(data);
   }

   public static void show(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] + "\t");
           }
           System.out.println();
       }
   }

   //数组转置操作方法
   public static void reverse(int temp[][]) {
       for (int x = 0; x < temp.length; x++) {
           for (int y = x; y < temp[x].length; y++) {
               if (x != y) {//行和列相同,进行交换
                   int num = temp[x][y];
                   temp[x][y] = temp[y][x];
                   temp[y][x] = num;
               }
           }
       }
   }
}
复制代码
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值