数组的值引用、地址引用

得意时要看淡,失意时要看开。不论得意失意,切莫大意;不论成功失败,切莫止步。志得意满时,需要的是淡然,给自己留一条退路;失意落魄时,需要的是泰然,给自己觅一条出路数组的值引用、地址引用,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

昨天在修改数组中某个对象里面的值的时候,发现修改值时,数组对应的值也会改变,于是就做了一下几个测试,发现除了String的数组不会改之外,其他都会发生改变。我打印了一下数组某个index的hashCode值以及接收该index的对象的hashcode值,只有String不一样,结果如下:

//示例1
JSONArray ja = new JSONArray();
JSONObject jo = new JSONObject();
jo.put("username", "zs");
jo.put("age", 19);
ja.add(jo);
JSONObject jo1 = ja.getJSONObject(0);
jo1.remove("age");
jo1.put("age", 18);
logger.info("ja===>[{}]", ja.get(0).hashCode());
logger.info("jo1===>[{}]", jo1.hashCode());
logger.info("ja====>[{}]", ja);

//打印结果:
15:04:16.787 [main] INFO com.ky.api.camera.TestThread2 - ja===>[-265617188]
15:04:16.793 [main] INFO com.ky.api.camera.TestThread2 - jo1===>[-265617188]
15:04:16.793 [main] INFO com.ky.api.camera.TestThread2 - ja====>[[{"age":18,"username":"zs"}]]

//示例2
ArrayList<EncodeDevInfo> infos = new ArrayList<EncodeDevInfo>();
EncodeDevInfo info = new EncodeDevInfo();
info.setIp("12");
infos.add(info);
EncodeDevInfo info1 = infos.get(0);
info1.setIp("22");
logger.info("addr===>[{}]", infos.get(0).hashCode());
logger.info("info1===>[{}]", info1.hashCode());
logger.info("infos====>[{}]", infos);
//打印结果
15:04:16.881 [main] INFO com.ky.api.camera.TestThread2 - addr===>[461583145]
15:04:16.881 [main] INFO com.ky.api.camera.TestThread2 - info1===>[461583145]
15:04:16.881 [main] INFO com.ky.api.camera.TestThread2 - infos====>[[EncodeDevInfo(xaddr=null, addtype=null, ip=22, devmanu=null, transmode=null, uuid=null, prototype=null, devtype=null, rtspurl=null, password=null, guid=null, devname=null, typename=null, username=null)]]


//示例3
ArrayList<String> strs = new ArrayList<String>();
strs.add("张三");
String str = strs.get(0);
str = "李四";
logger.info("strs===>[{}]", strs.get(0).hashCode());
logger.info("str===>[{}]", str.hashCode());
logger.info("strs====>[{}]", strs);
//打印结果:
15:04:16.882 [main] INFO com.ky.api.camera.TestThread2 - strs===>[774889]
15:04:16.882 [main] INFO com.ky.api.camera.TestThread2 - str===>[842061]
15:04:16.882 [main] INFO com.ky.api.camera.TestThread2 - strs====>[[张三]]

//示例4
ArrayList<EncodeDevInfo> devInfos = new ArrayList<EncodeDevInfo>();
EncodeDevInfo devInfo = new EncodeDevInfo();
devInfo.setIp("33");
devInfos.add(devInfo);

EncodeDevInfo devInfo1 = new EncodeDevInfo();
devInfo1.setIp("55");
devInfo1 = devInfos.get(0);
devInfo1.setIp("44");
logger.info("devInfos===>[{}]", devInfos.get(0).hashCode());
logger.info("devInfo1===>[{}]", devInfo1.hashCode());
logger.info("infos====>[{}]", devInfos);

//打印结果

15:04:16.882 [main] INFO com.ky.api.camera.TestThread2 - devInfos===>[-713118743]
15:04:16.882 [main] INFO com.ky.api.camera.TestThread2 - devInfo1===>[-713118743]
15:04:16.882 [main] INFO com.ky.api.camera.TestThread2 - infos====>[[EncodeDevInfo(xaddr=null, addtype=null, ip=44, devmanu=null, transmode=null, uuid=null, prototype=null, devtype=null, rtspurl=null, password=null, guid=null, devname=null, typename=null, username=null)]]

//示例5
JSONObject recode = new JSONObject();
recode.put("username", "zs");
EncodeDevInfo info = new EncodeDevInfo();
info.setIp("23");
recode.put("info", info);
JSONObject infoObj = recode.getJSONObject("info");
infoObj.put("ip", "44");
logger.info("recode====>[{}]", recode);
//打印结果:
15:16:25.458 [main] INFO com.ky.api.camera.TestThread2 - recode====>[{"username":"zs","info":{"ip":"23"}}]

解释:

因为String创建对象时会单独开辟一块内存,所以String创建的对象只是copy了数组中的值,所以修改String的值并不会修改数组中的值。但是其他示例中创建对象时,会将地址引用传给所 创建的对象,因此修改创建对象中属性的值时,会相应的修改数组中的值。(下面是 源码)

    /**
     * Returns the element at the specified position in this list.
     *
     * @param  index index of the element to return
     * @return the element at the specified position in this list
     * @throws IndexOutOfBoundsException {@inheritDoc}
     */
    public E get(int index) {
        rangeCheck(index);

        return elementData(index);
    }

    @SuppressWarnings("unchecked")
    E elementData(int index) {
        return (E) elementData[index];
    }

    transient Object[] elementData;

 

 

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/158168.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!