System.arraycopy方法使用

System.arraycopy方法使用,第1张

System.arraycopy方法使用

System.arraycopy方法使用

System提供了一个静态方法arraycopy(),我们可以使用它来实现数组之间的复制。

其函数原型是:

public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)

参数解释:

src:源数组;

srcPos:源数组要复制的起始位置;

dest:目的数组;

destPos:目的数组放置的起始位置;

length:复制的长度。

注意:src and dest都必须是同类型或者可以进行转换类型的数组.

(相关视频教程分享:java视频教程)

测试类:

public class SysTest {
 
    public static void main(String[] args) {
        String src[] = new String[] { "hello", "huang", "bao", "kang" };
        String dest[] = new String[5];
        System.arraycopy(src, 0, dest, 0, 4);
        for (String str : dest) {
            System.out.println(str);
        }
        System.out.println("=========华丽的分割线=========");
        System.arraycopy(src, 0, src, 1, 3);
        for (String str : src) {
            System.out.println(str);
        }
    }
}

控制台输出结果:

hello
huang
bao
kang
null
=========华丽的分割线=========
hello
hello
huang
bao

以上就是System.arraycopy方法使用的详细内容,

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

原文地址: https://outofmemory.cn/langs/688379.html

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

发表评论

登录后才能评论

评论列表(0条)

保存