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

...全文
53 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
  • 打赏
  • 举报
回复
孤独地顶着~~~
在Windows 10或Windows 11操作系统中,用户经常会遇到共享打印机时出现的一系列错误代码,这些错误代码可能会阻碍打印机共享功能的正常使用。常见的错误代码包括0x00000057、0x00000709和0x0000011b,这些代码通常指出了不同的问题,比如权限不足、服务未运行或配置错误等。除此之外,还有一些故障提示如“连接失败”或“内存不足”,这些都可能影响到打印机共享的稳定性。 要解决这些故障,首先要确保打印机已经正确地连接到网络,并且在需要共享的电脑上进行了设置。确保打印机驱动程序是最新的,并且在共享设置中没有错误配置。对于权限问题,需要检查网络上的用户账户是否具有足够的权限来访问共享打印机。同时,也要确保打印机服务正在运行,特别是“Print Spooler”服务,因为这是打印机共享服务的核心组件。 在某些情况下,问题可能与操作系统的更新有关,如升级到最新版的Windows 10或Windows 11后可能出现的兼容性问题。这时,可能需要查看微软的官方支持文档来获取特定的解决方案或更新。 对于错误代码0x00000057,这通常是由于没有足够的权限来访问网络打印机或其共享资源,解决方法是确保网络打印机的权限设置正确,包括在组策略中设置相应的访问权限。而0x00000709错误可能是由于打印机驱动问题或打印机端口配置错误,可以尝试重新安装或更新打印机驱动来解决。至于0x0000011b错误,这往往是因为打印机队列服务的问题,检查并重启“Print Spooler”服务通常是解决这类问题的常见手段。 至于“连接失败”或“内存不足”这类故障,通常与客户端和打印机之间的网络连接以及打印机本地资源的使用情况有关。检查网络连接,确保打印机所在的网络段没有故障或中断。同时,如果打印机的打印队列长时间得不到处理,可能会导致内存不足的情况,这时可能需要清理打印队列或增加打印机的内存配置。 为了帮助用户更快速地解决这些问题,市面上出现了各种打印机共享错误修复工具。这些工具往往通过预设的修复程序来自动检测和修正打印机共享中常见的问题。它们可以快速检查打印机驱动、网络连接以及共享设置,并且能够提供一键修复功能,大幅减少了用户自行排查和解决问题的难度。 然而,在使用这些修复工具之前,用户应确保这些工具的来源是安全可靠的,避免因使用不当的修复工具而引发其他系统安全或隐私问题。用户可以到官方平台或者信誉良好的软件提供商处下载这些工具。通过细心检查打印机的共享设置,及时更新驱动程序和服务,以及合理使用修复工具,大多数共享打印机的问题都可以得到有效的解决

23,409

社区成员

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

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