美文网首页
31-数组转置案例分析

31-数组转置案例分析

作者: c88bc9f9d088 | 来源:发表于2020-11-08 22:37 被阅读0次

    数组的反转操作指的是进行前后转置处理,即:首尾交换,例如,现在有一个数组,其内容如下:

  • 数组内容:1、2、3、4、5、6、7、8、9;

  • 交换后的内容:9、8、7、6、5、4、3、2、1;

    对于数组的前后交换有两种做法:

    做法一:定义一个新的数组而后按照逆序的方式保存(会产生无用的垃圾空间)

class ArrayUtil{
    public static void printArray(int temp[]){
        for(int x = 0; x < temp.length; x++){
            System.out.print(temp[x]+"、");
        }
    }
}
public  class ArrayDemo {
    public static void main(String args[]){
        int data[] = new int[]{1,2,3,4,5,6,7,8,9};
        int temp[] = new int[data.length];   //第二个数组
        int foot = temp.length - 1;  //第二个数组的脚标
        for(int x = 0; x < data.length; x++){
            temp[foot--] = data[x];
        }
        data = temp;
        ArrayUtil.printArray(data);
    }
}

    下面进行一下内存的分析处理,观察程序存在的问题。

做法二:在一个数组上进行转置

    现在如果要想实现这种转置最需要确定的就是数组转换的次数,次数的计算:“数组长度 ÷ 2”,实际上并不需要去考虑数组是奇数个数还是偶数个数。

class ArrayUtil{
    public static void printArray(int temp[]){
        for(int x = 0; x < temp.length; x++){
            System.out.print(temp[x]+"、");
        }
    }
}
public  class ArrayDemo {
    public static void main(String args[]){
        int data[] = new int[]{1,2,3,4,5,6,7,8,9};
        int center = data.length / 2 ; //确定转换的次数
        int head = 0; //操作脚标
        int tail = data.length - 1;  //操作脚标
        for(int x = 0; x < center; x++){ // 交换数据
            int temp = data[head]; 
            data[head] = data[tail];
            data[tail] = temp;
            head ++;
            tail --;
        }
        ArrayUtil.printArray(data);
    }
}

    两种实现如果要进行比较可以发现,第一种处理方式循环次数较多,并且还会产生垃圾,而第二种实现循环次数降低,但是存在有if判断增加了时间复杂度,可是可以减少无用对象的产生,以提升性能。

范例:将转换功能变为类定义

class ArrayUtil{
    public static void reverse(int data[]){
        int center = data.length / 2 ; //确定转换的次数
        int head = 0; //操作脚标
        int tail = data.length - 1;  //操作脚标
        for(int x = 0; x < center; x++){ // 交换数据
            int temp = data[head]; 
            data[head] = data[tail];
            data[tail] = temp;
            head ++;
            tail --;
        }
    }
    public static void printArray(int temp[]){
        for(int x = 0; x < temp.length; x++){
            System.out.print(temp[x]+"、");
        }
    }
}
public  class ArrayDemo {
    public static void main(String args[]){
        int data[] = new int[]{1,2,3,4,5,6,7,8,9};
        ArrayUtil.reverse(data); //转置处理
        ArrayUtil.printArray(data);
    }
}

    可以发现数组由于可以通过脚标进行元素的控制,所以相应的循环逻辑使用的会比较多。

相关文章

  • 31-数组转置案例分析

        数组的反转操作指的是进行前后转置处理,即:首尾交换,例如,现在有一个数组,其内容如下: 数组内容:1、2、...

  • Vue 纵向Table转横向Table (转置)

    数组参照矩阵思想, 对数组进行转置。 缺点: 转置后的数组仅是单纯的存每一行数据的数组用于展示, 失去了原数组...

  • 数组

    二维不规则数组的打印 打印数组结构 数组的冒泡排序 对一列数组进行简单的两两比较升序排列 结果 数组转置 数组转置...

  • python numpy数组转置和矩阵乘法

    末尾有福利(手动滑稽~) 一.转置 数组转置 1.使用 array.T: 1.对于array对象,若是一维数组(行...

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

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

  • numpy之转置(transpose)和轴对换

    转置(transpose)和轴对换 转置可以对数组进行重置,返回的是源数据的视图(不会进行任何复制操作)。 转置有...

  • Learn_for_Numpy

    数组转置和交换轴 转置 矩阵的行列交换 1. numpy.where (x if condition else ...

  • PHP 工具之数组

    二维数组值 二维数组翻转 二维数组转置

  • 2018-12-02

    Java二维数组的遍历及其转置详解 1.首先我们需要定义一个二维数组 2.对数组进行遍历 3.对数组进行转置操作 ...

  • 数组原地交换

    例1 旋转图像 例2 矩阵转置 将一个m*n的矩阵存储在一个一维数组中,原地实现矩阵的转置。分析:以一个4x2的矩...

网友评论

      本文标题:31-数组转置案例分析

      本文链接:https://www.haomeiwen.com/subject/qbdkbktx.html