菜鸟求救 关于JAVA容器的问题 在线等....

jessesongshuqun 2005-01-13 06:30:24
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 ]"
});*/
}
} ///:~
此程序编译运行都没问题,但程序中一条语句 a.add(new MyType(i));不明白,MyType是什么类型??
...全文
132 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
shan1119 2005-01-14
  • 打赏
  • 举报
回复
有问题
fog628 2005-01-14
  • 打赏
  • 举报
回复
盗版的thinking in java

呵呵
jessesongshuqun 2005-01-14
  • 打赏
  • 举报
回复
是thinking in java上的代码 我没有定义 书上也找不到啊 怎么办 编译运行都没问题
不徻写代码 2005-01-14
  • 打赏
  • 举报
回复
嘿嘿,MyType我的类型
xiantongsky 2005-01-14
  • 打赏
  • 举报
回复
a.add(new MyType(i));
是向一个容器(TreeSet)中添加对象MyType.
MyType是你自己定义的类,
一定有定义不然会编译通不过的,你找一下.
fog628 2005-01-14
  • 打赏
  • 举报
回复
class MyType implements Comparable {
private int i;
public MyType(int n) { i = n; }
public boolean equals(Object o) {
return
(o instanceof MyType)
&& (i == ((MyType)o).i);
}
public int hashCode() { return i; }
public String toString() { return i + " "; }
public int compareTo(Object o) {
int i2 = ((MyType)o).i;
return (i2 < i ? -1 : (i2 == i ? 0 : 1));
}
}


是thinking in java上的代码吧
jessesongshuqun 2005-01-14
  • 打赏
  • 举报
回复
有人帮忙吗
jessesongshuqun 2005-01-13
  • 打赏
  • 举报
回复
有高手吗 帮忙一下啊
ttfy1234 2005-01-13
  • 打赏
  • 举报
回复
鬼啊~~
jessesongshuqun 2005-01-13
  • 打赏
  • 举报
回复
编译运行都没问题
vgvg 2005-01-13
  • 打赏
  • 举报
回复
你可以编译通过??
jessesongshuqun 2005-01-13
  • 打赏
  • 举报
回复
我电脑里根本就找不到MyType
vgvg 2005-01-13
  • 打赏
  • 举报
回复
a.add(new MyType(i));
是向一个容器(TreeSet)中添加对象MyType.
MyType是你自己定义的类,
一定有定义不然会编译通不过的,你找一下.
zhang52766 2005-01-13
  • 打赏
  • 举报
回复
關注.

62,612

社区成员

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

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