数组拷贝System.arraycopy

数组拷贝

第一种方式:

package com.coding.demo.concurrent;

import java.util.Arrays;

/**
 * 使用Arrays.copyOf()
 */
public class TestArraysCopyOf {
    public static void main(String[] args) {
        int[] src = {1,2,3,4,5,6,7,8,9};
        int[] dest = Arrays.copyOf(src, src.length);
        System.out.println(Arrays.toString(dest));
        // output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
    }
}

第二种方式:

package com.coding.demo.concurrent;

import java.util.Arrays;

/**
 * 使用System.arraycopy
 */
public class TestSystemArrayCopyOf {
    public static void main(String[] args) {
        int[] src = {1,2,3,4,5,6,7,8,9};
        int[] dest = new int[10];
        //   * @param      src      the source array.原数组
        //    * @param      srcPos   starting position in the source array.从原数组的哪个位置进行拷贝,默认下标从0开始。
        //    * @param      dest     the destination array.目标数组
        //    * @param      destPos  starting position in the destination data.从目标数组的哪个位置开始存放
        //    * @param      length   the number of array elements to be copied.要拷贝多少个元素到新数组中,
        // 如果新数组的长度10,但是我就从原来的数组中拿8个元素拷贝到新数组中,新数组剩余的两个位置将会用0来填补。
        System.arraycopy(src,1, dest, 0, src.length - 1);
        System.out.println(Arrays.toString(dest));
        // output: [2, 3, 4, 5, 6, 7, 8, 9, 0, 0]
    }
}java

posted on   ~码铃薯~  阅读(12)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
历史上的今天:
2022-08-13 Netty学习

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示