String a = new String("a");
String b = new String("b");
String a2 = new String("a");
Collection col = new HashSet();
col.add(a);
col.add(b);
// col.add(a2);
a = "";
col.remove(a);
// col.remove(a2);
System.out.println(col);
String b = new String("b");
String a2 = new String("a");
Collection col = new HashSet();
col.add(a);
col.add(b);
// col.add(a2);
a = "";
col.remove(a);
// col.remove(a2);
System.out.println(col);
解决方案 »
- GUI里面的面板可以是透明的吗?
- 从数据库中取数据,想写个批处理但不知道批处理后怎么获得取到的值?
- JAVA和C++的关系
- 新手提问,简单java编程问题。
- 解决立即结贴 java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Object has been closed.
- 怎样把一个字符复制N次,如 'a'变成'aaaaa'
- jfreechart的同仁进来发表下意见
- 这个正则表达式怎么写啊?
- 我怎么觉得抽象类和接口是一回事,是吗?
- Socket传输中文字符处理!!高分求救
- 有关内部类声明为public的不明之处
- 这个递归哪里出了问题?
System.out.println( col.remove(a));
//false
Result:
[b, a]
因为a已经被重新赋值,代表一个新的字符串。
因为 String a = new String("a");
String b = new String("b");
String a2 = new String("a");
Collection col = new HashSet();
col.add(a);
col.add(b);/*这个时候col对象中包含了两个String元素"a","b"*/
// col.add(a2);
a = ""; /*更改a引用类型指向的对象*/
col.remove(a);/*从col中移除a引用所指向的对象,因为col中不包含"",所以remove失败*/
// col.remove(a2);
System.out.println(col);/*结果[b,a]*/