java arraycopy 的疑问
伊谢尔伦
伊谢尔伦 2017-04-18 10:04:14
[Java讨论组]
public class ArrayCopy {
    public static void main(String[] args) {
        String[] s = {"M", "I", "S", "O", "A"};
        String[] b = new String[6];
        System.arraycopy(s, 0, b, 0, s.length);
        b[2] = "asdasd";
        System.out.println(s[2]);//s
        System.out.println(b[2]);//asdasd
        
        int[][] a = {{1, 2, 3}, {2, 4, 5}, {1, 3, 4}};
        int[][] a2 = new int[3][];
        System.arraycopy(a, 0, a2, 0, a.length);
        a2[0][0] = 12;
        System.out.println(a[0][0]);//12
    }
}

二维数组拷贝后拷贝后的数组改变拷贝前的数组也改变,但为什么String类型的数组不会有同样的现象呢。不都是引用对象吗。

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(1)
迷茫

你好好运行一下看看,第二个输出是:1,不是12。。。。

public class Test {
    public static void main(String[] args) {
        String[] s = { "M", "I", "S", "O", "A" };
        String[] b = new String[6];
        System.arraycopy(s, 0, b, 0, s.length);
        b[2] = "asdasd";
        System.out.println(s[2]);// s
        System.out.println(b[2]);// asdasd

        int[][] a = { { 1, 2, 3 }, { 2, 4, 5 }, { 1, 3, 4 } };
        int[][] a2 = new int[3][3];
        System.arraycopy(a[0], 0, a2[0], 0, a[0].length);
        a2[0][0] = 12;
        System.out.println(a[0][0]);// 1
    }
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号