本人正在看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

...全文
54 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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
  • 打赏
  • 举报
回复
孤独地顶着~~~
内容概要:本文档是2025年全国广播电视技术能手竞赛IPTV专业的竞赛内容大纲,旨在指导参赛选手准备比赛。竞赛分为理论和实操两大部分。理论部分涵盖基本知识、基础理论和专业知识,包括法律法规、技术标准、IPTV技术基础、信号传输与分发、网络和数据安全等。实操部分则涉及IPTV系统设备、仪器设备的操作使用和指标测量。具体内容包括IPTV系统架构、传输技术与指标、设备配置及维护、专业技能如测量工具使用、视音频指标测量、直播点播协议分析、播出网络性能测量、网络安全数据分析以及系统故障排查等。 适合人群:从事或有意从事IPTV相关工作的技术人员,尤其是有志于参加全国广播电视技术能手竞赛的专业人士。 使用场景及目标:①帮助参赛选手全面了解并掌握IPTV相关的法规文件和技术标准;②提升选手对IPTV系统架构、传输技术和设备配置的理解;③增强选手在实际操作中的测量、分析和故障排查能力;④确保选手能够熟练运用各种测量工具和分析软件进行视音频指标测量和网络安全数据分析。 阅读建议:由于文档内容详尽且专业性强,建议读者在学习过程中结合实际案例和操作练习,同时参考相关技术文献,以便更好地理解和掌握竞赛所需的知识和技能。

23,409

社区成员

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

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