陣列的值引用 位址引用

2021-10-23 10:03:50 字數 4182 閱讀 2252

昨天在修改陣列中某個物件裡面的值的時候,發現修改值時,陣列對應的值也會改變,於是就做了一下幾個測試,發現除了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====>

//示例2

arraylistinfos = new arraylist();

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

arrayliststrs = new arraylist();

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

arraylistdevinfos = new arraylist();

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====>[}]

解釋:

因為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 indexoutofbound***ception

*/public e get(int index)

@suppresswarnings("unchecked")

e elementdata(int index)

transient object elementdata;

引用位址小練習

var student function var bosn new student student.prototype.x 101 student.prototype console.log bosn.x 今天在網上看到了上面這個例子,認為console.log bosn.x 值為undefined...

指標引用位址的筆記

位址 在c 中,位址標號使用十六進製制表示。取乙個變數的位址使用 符號,只有變數 才存在記憶體位址,常量沒有位址 不包括const定義的偽常量 例如,對於數 字100,我們無法取出它的位址。取出的位址是一 個常量值,無法再對其取位址了。2.指標 指標的定義使用 type type 為資料型別,任何資...

c語言傳值呼叫,引用呼叫,位址呼叫

指標是c語言中比較難理解的部分。尤其是對於 的使用不是很清楚 下面我們講看看幾個小例子。在這裡插入 片 int n 5 printf n d n n int b n printf b d n b printf n位址 d n n printf b位址 d n b 結果 n 5 b 5n位址 6487...