最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java 引用類型的數(shù)據(jù)傳遞的是內(nèi)存地址實(shí)例

 更新時(shí)間:2021年10月27日 08:58:33   作者:慕容囧囧  
這篇文章主要介紹了java 引用類型的數(shù)據(jù)傳遞的是內(nèi)存地址實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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)文章

最新評(píng)論

舒兰市| 康保县| 天等县| 盱眙县| 峨眉山市| 东兴市| 扎鲁特旗| 茶陵县| 台湾省| 明溪县| 启东市| 贡觉县| 镇沅| 天祝| 南丹县| 抚州市| 庆元县| 磐安县| 通化县| 广南县| 旌德县| 静宁县| 马山县| 文化| 安顺市| 元谋县| 沂水县| 石河子市| 启东市| 永胜县| 镇巴县| 蕉岭县| 志丹县| 霍林郭勒市| 香港 | 萨嘎县| 保山市| 汉寿县| 永善县| 斗六市| 增城市|