java 引用類型的數(shù)據(jù)傳遞的是內(nèi)存地址實(shí)例
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("張無(wú)忌", 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 = {"贛州市", "于都縣", "江西省", "中國(guó)", "贛南地區(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ù)類型時(shí)的問題
集合list的存儲(chǔ)元素
(1)如果list中存的是基本數(shù)據(jù)類型,則存的是值
(2)如果list中存的是引用數(shù)據(jù)類型(如對(duì)象等),則存的是引用
如果對(duì)象的引用地址沒變,內(nèi)容變化,也會(huì)引起調(diào)用該對(duì)象時(shí)內(nèi)容變化
下面舉一個(gè)在集合list中存放對(duì)象的例子:
//構(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中存儲(chǔ)對(duì)象的引用所指的內(nèi)容發(fā)生了變化,所以導(dǎo)致了list中元素內(nèi)容的改變
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?Cloud?Gateway整合sentinel?實(shí)現(xiàn)流控熔斷的問題
本文給大家介紹下?spring?cloud?gateway?如何整合?sentinel實(shí)現(xiàn)流控熔斷,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友一起看看吧2022-02-02
Java實(shí)現(xiàn)List去重的幾種方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了Java中List去重的幾種常用方法總結(jié),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)和參考價(jià)值,需要的小伙伴可以了解一下2023-09-09
mybatis中實(shí)現(xiàn)枚舉自動(dòng)轉(zhuǎn)換方法詳解
在使用mybatis的時(shí)候經(jīng)常會(huì)遇到枚舉類型的轉(zhuǎn)換,下面這篇文章主要給大家介紹了關(guān)于mybatis中實(shí)現(xiàn)枚舉自動(dòng)轉(zhuǎn)換的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-08-08
在Java的Struts中判斷是否調(diào)用AJAX及用攔截器對(duì)其優(yōu)化
這篇文章主要介紹了在Java的Struts中判斷是否調(diào)用AJAX及用攔截器對(duì)其優(yōu)化的方法,Struts框架是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2016-01-01
java編程實(shí)現(xiàn)國(guó)際象棋棋盤
這篇文章主要為大家詳細(xì)介紹了java編程實(shí)現(xiàn)國(guó)際象棋棋盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
JDK數(shù)組阻塞隊(duì)列源碼深入分析總結(jié)
在這篇文章當(dāng)中,我們將通過(guò)源碼仔細(xì)為大家介紹一下JDK具體是如何實(shí)現(xiàn)數(shù)組阻塞隊(duì)列的,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-08-08
java根據(jù)開始時(shí)間結(jié)束時(shí)間計(jì)算中間間隔日期的實(shí)例代碼
這篇文章主要介紹了java根據(jù)開始時(shí)間結(jié)束時(shí)間計(jì)算中間間隔日期的實(shí)例代碼,需要的朋友可以參考下2019-05-05

