本人正在看TIJ3,碰到一个问题,如果你能解决这个问题并且正在看TIJ的话进来加个好友

JCC0128 2003-12-18 11:11:51
9.帮我看看这个输出,怎么我的Set中存在有重复元素。

A君答:你需要在class MyType实现public int hashCode()这个方法。
B君答:在类MyType重定义equals或compareTo方法试一下。

代码如下:
import java.util.*;
/**
* @author Administrator
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/

class MyType{
private int i ;
public MyType(int i){
this.i = i ;
}

public String toString(){
return "MyType id:"+ i ;
}

}

public class Set2 {
private static Set fill(Set a , int size){
for(int i = 0 ;i
a.add(new MyType(i)) ;
}
return a ;

}

public static void test(Set a){
fill(a , 10) ;
System.out.println(a) ;
fill(a , 10) ;
System.out.println(a) ;
}

public static void main(String[] args) {
test(new HashSet()) ;

}
}

输出结果如下:
[MyType id:9, MyType id:1, MyType id:3, MyType id:5, MyType id:2, MyType id:7, MyType id:0, MyType id:8, MyType id:4, MyType id:6]
[MyType id:9, MyType id:4, MyType id:3, MyType id:1, MyType id:8, MyType id:9, MyType id:3, MyType id:2, MyType id:5, MyType id:2, MyType id:7, MyType id:5, MyType id:0, MyType id:1, MyType id:8, MyType id:6, MyType id:0, MyType id:6, MyType id:7, MyType id:4]


TIJ3的那一节没看完,再接着看好像就有答案了~~~

msn: liuyaoliusos@hotmail.com

...全文
36 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ozone 2003-12-21
  • 打赏
  • 举报
回复
equals
hasCode
都要定义吧,记不太清楚了
yoken 2003-12-20
  • 打赏
  • 举报
回复
tij中相关内容:

When creating your own types, be aware that a Set needs a way to maintain a storage order, which means that you must implement the Comparable interface and define the compareTo( ) method. Here’s an example: Feedback


//: c11:Set2.java
// Putting your own type in a Set.
import com.bruceeckel.simpletest.*;
import java.util.*;

public class Set2 {
private static Test monitor = new Test();
public static Set fill(Set a, int size) {
for(int i = 0; i < size; i++)
a.add(new MyType(i));
return a;
}
public static void test(Set a) {
fill(a, 10);
fill(a, 10); // Try to add duplicates
fill(a, 10);
a.addAll(fill(new TreeSet(), 10));
System.out.println(a);
}
public static void main(String[] args) {
test(new HashSet());
test(new TreeSet());
test(new LinkedHashSet());
monitor.expect(new String[] {
"[2 , 4 , 9 , 8 , 6 , 1 , 3 , 7 , 5 , 0 ]",
"[9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 ]",
"[0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]"
});
}
} ///:~



The form for the definitions for equals( ) and hashCode( ) will be described later in this chapter. You must define an equals( ) in both cases, but the hashCode( ) is absolutely necessary only if the class will be placed in a HashSet (which is likely, since that should generally be your first choice as a Set implementation). However, as a programming style, you should always override hashCode( ) when you override equals( ). This process will be fully detailed later in this chapter. Feedback




yoken 2003-12-20
  • 打赏
  • 举报
回复
Set中add的都是new MyType(i),jvm怎么知道谁跟谁什么关系,这需要你自己规定,有两种方法:1、两个MyType object相等的条件是两者的hashcode一样
2、两个MyType object相等通过equals()来定义(private int i值相同,两个object才相同),j2sdk documentation里对java.util.Set有如下说明:
public boolean add(Object o)
Adds the specified element to this set if it is not already present (optional operation). More formally, adds the specified element, o, to this set if this set contains no element e such that (o==null ? e==null : o.equals(e)). If this set already contains the specified element, the call leaves this set unchanged and returns false. In combination with the restriction on constructors, this ensures that sets never contain duplicate elements.
ddbean 2003-12-20
  • 打赏
  • 举报
回复
......
cloudtarget 2003-12-20
  • 打赏
  • 举报
回复
?
JCC0128 2003-12-18
  • 打赏
  • 举报
回复
哪里有免费的 TIJ3的源码下载,谢谢!
JCC0128 2003-12-18
  • 打赏
  • 举报
回复
孤独地顶着~~~

23,402

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧