首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

区别,该怎么处理

2012-01-09 
区别Map map11122new HashMap()map11122.put(1, A)Map map11133nullmap11133map11122System.ou

区别
Map map11122=new HashMap();
map11122.put("1", "A");
Map map11133=null;
map11133=map11122;
System.out.println("11111---"+map11133.toString());

Map map111221=new HashMap();
map111221.put("1", "A");
Map map111331=new HashMap();
map111331=map111221;
System.out.println("22222---"+map111331.toString());

[解决办法]
逻辑实现上没有任何区别,因为后来你吧
map11133=map11122;
map111331=map111221;
事实上导致两个对象引用都是指向另外的对象,而另外两个对象的数据是一样的
你用Map map111331=new HashMap();只是新建了一个对象,将引用指向整个对象,但是整个对象从来没被使用,后来的引用指向了其他对象
Map map111331=null 只是声明,没有新建对象
后来的引用指向了其他对象

探讨
我是想问题对于
Map map11122=new HashMap();
map11122.put("1", "A");
Map map11133=null;map11133=map11122;
System.out.println("11111---"+map11133.toString());

Map map111221=new HashMap();
map111221.put(……

[解决办法]
没有区别,又重新赋值map111331=map111221;
原来的创建的map111331对象被抛弃了

热点排行