java 复制数组

java 复制数组,第1张

java 复制数组
import java.util.*;

public class copyArray {
    public static void main(String[] args) {
        int[] e = {4,5,2,7,8};
        int[] f;
        f = Arrays.copyOf(e,3);// input 0-2
        for (int k = 0; k < f.length; k++) {
            System.out.print(f[k]+" ");
        }
        System.out.println();

        int[] a = {1, 2, 3, 4};
        int[] b = new int[10];
        b = Arrays.copyOfRange(a, 0, 4);
        for(int i = 0; i < 4; i++)
            System.out.print(b[i]+" ");
        System.out.println();


        int[] c = {4,5,2,7,8};
        int[] d;
        d = c.clone();
        for (int j = 0; j < d.length; j++) {
            System.out.print(d[j]+" ");
        }
        System.out.println();

        int[] g = {4,8,6,3,4,56};
        int[] h = new int[10];
        System.arraycopy(g,2,h,3,4);//A starts from 2 and copies from 0-3 to B starts from 3
        for (int n = 0; n < h.length; n++) {            //a从2开始往后0-3复制到b从3开始放入
            System.out.print(h[n]+" ");
        }

    }
}

输出结果:

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5710015.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存