java 引用類型的數(shù)據(jù)傳遞的是內(nèi)存地址實例
java 引用類型的數(shù)據(jù)傳遞的是內(nèi)存地址
java中引用類型的數(shù)據(jù),傳遞的是內(nèi)存地址,像類,數(shù)組,接口,String等等都是引用類型!
看下面的代碼和截圖
public class Test2 { // java中引用類型的數(shù)據(jù)傳遞的是內(nèi)存地址 private Map<String, Student> students = new Hashtable<String, Student>(); public void myTest() { Student student1 = new Student("令狐沖", 16, "華山派", 19888.66); Student student2 = new Student("韋小寶", 15, "紫禁城", 99999.99); Student student3 = new Student("張無忌", 18, "光明頂", 18888.88); students.put("1", student1); students.put("2", student2); students.put("3", student3); Iterator<Map.Entry<String, Student>> entries = students.entrySet().iterator(); while (entries.hasNext()) { Map.Entry<String, Student> entry = entries.next(); System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); entry.getValue().setAge(entry.getValue().getAge() + 1); } } public void myTest2(){ List<String[]> citys = new ArrayList<String[]>(); String [] cityNames = {"贛州市", "于都縣", "江西省", "中國", "贛南地區(qū)"}; citys.add(cityNames); for (int index = 0; index < citys.size(); index++) { String[] mycityNames = citys.get(index); for (int i = 0; i < mycityNames.length; i++) { System.out.println(mycityNames[i]); if (i == 1) { //修改mycityNames數(shù)組 (java中引用類型的數(shù)據(jù)傳遞的是內(nèi)存地址) mycityNames[i] = "我們" + mycityNames[i]; } } System.out.println("--------------------------------"); for (int i = 0; i < mycityNames.length; i++) { System.out.println(mycityNames[i]); } } System.out.println("--------------------------------"); //修改cityNames數(shù)組 (java中引用類型的數(shù)據(jù)傳遞的是內(nèi)存地址) cityNames[0] = "我們贛州市"; for (int index = 0; index < citys.size(); index++) { String[] mycityNames = citys.get(index); for (int i = 0; i < mycityNames.length; i++) { System.out.println(mycityNames[i]); } } } public static void main(String[] args) { Test2 test2 = new Test2(); test2.myTest(); System.out.println("--------------------------------"); Iterator<Map.Entry<String, Student>> entries = test2.students.entrySet().iterator(); while (entries.hasNext()) { Map.Entry<String, Student> entry = entries.next(); System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); } test2.myTest2(); } }
list集合存引用數(shù)據(jù)類型時的問題
集合list的存儲元素
(1)如果list中存的是基本數(shù)據(jù)類型,則存的是值
(2)如果list中存的是引用數(shù)據(jù)類型(如對象等),則存的是引用
如果對象的引用地址沒變,內(nèi)容變化,也會引起調(diào)用該對象時內(nèi)容變化
下面舉一個在集合list中存放對象的例子:
//構(gòu)建原始數(shù)據(jù) ArrayList<Goods> list = new ArrayList(); Goods goods1=new Goods("plum ", 2); list.add(goods1); list.add(new Goods("apple", 1)); list.add(new Goods("banana", 2)); //打印原始數(shù)據(jù) System.out.println("原始數(shù)據(jù):"); for(Goods goods : list){ System.out.println(goods); } goods1.setGoodsId(444);//改變goods1的內(nèi)容 System.out.println("原始數(shù)據(jù)1:"); for(Goods goods : list){ System.out.println(goods); }
結(jié)果:
原始數(shù)據(jù):
Goods{goodsName='plum ', goodsId=2}
Goods{goodsName='apple', goodsId=1}
Goods{goodsName='banana', goodsId=2}
原始數(shù)據(jù)1:
Goods{goodsName='plum ', goodsId=444}
Goods{goodsName='apple', goodsId=1}
Goods{goodsName='banana', goodsId=2}
結(jié)果表明:由于list中存儲對象的引用所指的內(nèi)容發(fā)生了變化,所以導(dǎo)致了list中元素內(nèi)容的改變
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JPA @Basic單表查詢?nèi)绾螌崿F(xiàn)大字段懶加載
這篇文章主要介紹了JPA @Basic單表查詢?nèi)绾螌崿F(xiàn)大字段懶加載的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08Spring?Security中使用authorizeRequests遇到的問題小結(jié)
Spring?是非常流行和成功的?Java?應(yīng)用開發(fā)框架,Spring?Security?正是?Spring?家族中的成員,這篇文章主要介紹了Spring?Security中使用authorizeRequests遇到的問題,需要的朋友可以參考下2023-02-02Java之next()、nextLine()區(qū)別及問題解決
這篇文章主要介紹了Java之next()、nextLine()區(qū)別及問題解決,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08java算法題解LeetCode35復(fù)雜鏈表的復(fù)制實例
這篇文章主要為大家介紹了java算法題解LeetCode35復(fù)雜鏈表的復(fù)制實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01