Arrays.copyOf 函数

测试类如下

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 协议》,转载必须注明作者和本文链接

一个从事软件开发职业的经济爱好者

讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
未填写
文章
1
粉丝
1
喜欢
0
收藏
0
排名:2136
访问:5
私信
所有博文
博客标签