数组的值引用、地址引用

昨天在修改数组中某个对象里面的值的时候,发现修改值时,数组对应的值也会改变,于是就做了一下几个测试,发现除了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;

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值