Arrays.copyOf 函式

hayeka發表於2020-06-26

測試類如下

package com.x.summary;
//------------------------------------------------
//    
//  corleone
//  2020/6/26
//
//------------------------------------------------

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        int[] array = {0,1,2,3,4,5,6,8,9,10};
        int[] copy1 = Arrays.copyOf(array, 5);
        System.out.println(Arrays.toString(copy1));
        int[] copy2 = Arrays.copyOf(array, 10);
        System.out.println(Arrays.toString(copy2));
        int[] copy3 = Arrays.copyOf(array, 15);
        System.out.println(Arrays.toString(copy3));

        byte[] a = {1,2};
        short[] b = {1,2};
        int[] c = {1,2};
        long[] d = {1,2};
        char[] e = {'1','2'};
        boolean[] f= {true,false};
        float[] g = {1.1F,1.2F};
        double[] h = {1.1,1.2};

        String[] s = {"郭張馳","王展","yaosheng1","yaosheng2","yaosheng3"};
        One[] one = {};

        System.out.println(Arrays.toString(Arrays.copyOf(a,1)));
        System.out.println(Arrays.toString(Arrays.copyOf(b,1)));
        System.out.println(Arrays.toString(Arrays.copyOf(c,1)));
        System.out.println(Arrays.toString(Arrays.copyOf(d,1)));
        System.out.println(Arrays.toString(Arrays.copyOf(e,1)));
        System.out.println(Arrays.toString(Arrays.copyOf(f,1)));
        System.out.println(Arrays.toString(Arrays.copyOf(g,1)));
        System.out.println(Arrays.toString(Arrays.copyOf(h,1)));

        System.out.println(Arrays.toString(Arrays.copyOf(s,1)));
        System.out.println(Arrays.toString(Arrays.copyOf(one,1)));
    }
}

輸出如下

[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5, 6, 8, 9, 10]
[0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 0, 0, 0, 0, 0]

經過檢視,程式碼執行如下

  • 如果陣列為基礎型別,則直接呼叫
//基礎型別都是一樣的
//Arrays.copyOf方法
public static byte[] copyOf(byte[] original, int newLength) {
        byte[] copy = new byte[newLength];
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }
//System.arraycopy方法
public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);
  • 如果是引用型別,則先判斷型別,再呼叫
//若是引用資料型別的陣列
//Arrays.copyOf方法
public static <T> T[] copyOf(T[] original, int newLength) {
        return (T[]) copyOf(original, newLength, original.getClass());
    }
//呼叫這個方法
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
        @SuppressWarnings("unchecked")
        T[] copy = ((Object)newType == (Object)Object[].class)
            ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }
//然後呼叫System的arraycopy方法
public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);
本作品採用《CC 協議》,轉載必須註明作者和本文連結

一個從事軟體開發職業的經濟愛好者

相關文章