• java杂记——数组拷贝


    这里介绍两种java提供的数组拷贝方法:

    (1)Arrays提供的copyOf(T src, T desLength)和copyOfRange(T src, int from, int to)

    (2)System.arraycopy(T src, int from, T des, int from, int length)

     使用例子:

    (1)

    int [] a = {1,2,3,4,5,6};
    int [] b = Arrays.copyOf(a, 8); //把a拷贝到长度为8的b中。b的长度可比a长,长的部分补0;可比a短,把a截断。
    int [] c = Arrays.copyOfRange(a, 2, 5); //把a的数组下标为2的到下标为5的前一个,拷贝到c中,c的长度为5-2=3。

    (2)

    int [] a = {1,2,3,4,5,6};
    int [] b = new int[8];
    System.arraycopy(a, 2, b, 3, 4); //把数组a从下标为2的元素开始,拷贝长度为4的元素到b中,从b的下标为3的位置开始放。
                    //如果b的位置不足以放下指定长度的元素,会报java.lang.ArrayIndexOutOfBoundsException异常

    在方法(2)中,如果目标数组和源数组相同,则先拷贝到一个temp数组中,再从temp数组中拷回目标数组(源)。

    当数组是对象数组的时候,上面的两种方法都不能实现深度拷贝。例如:源代码中的

    Lesson9_arrayCopyTest.deepArrayCopy()

    详细代码如下:

    package javaBase16Lesson;
    
    import java.util.Arrays;
    
    /**
     * (1)
     *     int [] a = {1,2,3,4,5,6};
     *    int [] b = Arrays.copyOf(a, 8);    //把a拷贝到长度为8的b中。b的长度可比a长,长的部分补0;可比a短,把a截断。
     *    int [] c = Arrays.copyOfRange(a, 2, 5);  //把a的数组下标为2的到下标为5的前一个,拷贝到c中,c的长度为5-2=3。
     *
     *(2)
     *    int [] a = {1,2,3,4,5,6};
     *    int [] b = new int[8];
     *    System.arraycopy(a, 2, b, 3, 4);  //把数组a从下标为2的元素开始,拷贝长度为4的元素到b中,从b的下标为3的位置开始放。
     *                                                     //如果b的位置不足以放下指定长度的元素,会报java.lang.ArrayIndexOutOfBoundsException异常
     *
     *(3)
     *方法(1)(2)都只是数组的浅拷贝,无法实现深度拷贝
     * @author cnx
     * @Description : arrayCopyTest
     * @CreateDate ; 2014年7月9日 下午6:43:26
     */
    public class Lesson9_arrayCopyTest {
        public static void main(String[] args) {
    //        Lesson9_arrayCopyTest.arraysCopyOf();
    //        Lesson9_arrayCopyTest.systemArraycopy();
            Lesson9_arrayCopyTest.deepArrayCopy();
        }
        
        
        public static void arraysCopyOf(){
            int [] a = {1,2,3,4,5,6};
            int [] b = Arrays.copyOf(a, 8);    //把a拷贝到长度为8的b中。b的长度可比a长,长的部分补0;可比a短,把a截断。
            int [] c = Arrays.copyOfRange(a, 2, 5);  //把a的数组下标为2的到下标为5的前一个,拷贝到c中,c的长度为5-2=3。
            System.out.println("a:"+ a.length);
            for (int i = 0; i < a.length; i++) {
                System.out.println(a[i]);
            }
            System.out.println("b:"+ b.length);
            for (int i : b) {
                System.out.println(i);
            }
            System.out.println("c:"+ c.length);
            for (int i : c) {
                System.out.println(i);
            }
        }
        /**
         *         Copies an array from the specified source array, beginning at the specified position, 
         * to the specified position of the destination array. A subsequence of array components 
         * are copied from the source array referenced by src to the destination array referenced by dest. 
         * The number of components copied is equal to the length argument. The components at positions 
         * srcPos through srcPos+length-1 in the source array are copied into positions destPos through 
         * destPos+length-1, respectively, of the destination array. 
         *         If the src and dest arguments refer to the same array object, then the copying is performed 
         * as if the components at positions srcPos through srcPos+length-1 were first copied to a temporary 
         * array with length components and then the contents of the temporary array were copied into 
         * positions destPos through destPos+length-1 of the destination array. <br>
         * 如果目标数组和源数组相同,则先拷贝到一个temp数组中,再从temp数组中拷回目标数组(源)
         */
        public static void systemArraycopy(){
            int [] a = {1,2,3,4,5,6};
            int [] b = new int[8];
            System.arraycopy(a, 2, b, 3, 4);  //把数组a从下标为2的元素开始,拷贝长度为4的元素到b中,从b的下标为3的位置开始放。
                                                             //如果b的位置不足以放下指定长度的元素,会报java.lang.ArrayIndexOutOfBoundsException异常
            System.out.println("a:"+ a.length);
            for (int i = 0; i < a.length; i++) {
                System.out.println(a[i]);
            }
            System.out.println("b:"+ b.length);
            for (int i : b) {
                System.out.println(i);
            }
        }
        
        /**
         * 上面的两种方法都无法实现深度拷贝
         */
        public static void deepArrayCopy(){
            Lesson9_array1 [] a1 = {new Lesson9_array1(),new Lesson9_array1()}; 
            Lesson9_array1 [] a2 = null;
            Lesson9_array1 [] a3 = new Lesson9_array1[3];
            Lesson9_array2 [] b1 = {new Lesson9_array2(),new Lesson9_array2()}; 
            Lesson9_array2 [] b2 = null;
            
            
            a2 = Arrays.copyOf(a1, 3);
            System.arraycopy(a1, 0, a3, 0, 2);
            b2 = Arrays.copyOf(b1, 3);
            
            a1[1].a = "a1";
            System.out.println(a2[1].a);
            System.out.println(a3[1].a);
            b1[1].a = "b1";
            System.out.println(b2[1].a);
        }
    }
    
    
    class Lesson9_array1 implements Cloneable{
        public String a = "deep";
        
        public Object clone(){
            Object o = null;
            try {
                o = (Lesson9_array1)super.clone();
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
            return o;
        }
        
        public String toString(){
            return a;
        }
    }
    
    class Lesson9_array2{
        public String a = "deep";
        
        public String toString(){
            return a;
        }
    }
    作者:苍枫露雨
             
    本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    HDU 1501 Zipper(DFS)
    HDU 2181 哈密顿绕行世界问题(DFS)
    HDU 1254 推箱子(BFS)
    HDU 1045 Fire Net (DFS)
    HDU 2212 DFS
    HDU 1241Oil Deposits (DFS)
    HDU 1312 Red and Black (DFS)
    HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
    HDU 1022 Train Problem I(栈)
    HDU 1008 u Calculate e
  • 原文地址:https://www.cnblogs.com/chrischennx/p/3834440.html
Copyright © 2020-2023  润新知