我在使用我的自定义集 MySet 时遇到问题,使用联合和相交的基本功能。该程序编译没有错误,但只返回一个空集。
有人看到问题出在哪里了吗?
public class MySet<E> extends TreeSet<E> {
Set<E> set;
public MySet(){
set = null;
}
public MySet(Set<E> set){
this.set = set;
}
public void union(Set<E> s){
set.addAll(s);
}
public void intersection(Set<E> s){
set.retainAll(s);
}
}
主要方法
public class TestSet {
public static void main(String[] args) throws FileNotFoundException{
File f1 = new File("courseList1.txt");
File f2 = new File("courseList2.txt");
Scanner scan1 = new Scanner(f1);
Scanner scan2 = new Scanner(f2);
Set<Coarse> set1 = new HashSet<Coarse>();
Set<Coarse> set2 = new HashSet<Coarse>();
MySet<Coarse> mySet = new MySet<Coarse>(set1);
String designator;
int number;
while(scan1.hasNext()){
designator = scan1.next();
number = scan1.nextInt();
set1.add(new Coarse(designator, number));
}
while(scan2.hasNext()){
designator = scan2.next();
number = scan2.nextInt();
set2.add(new Coarse(designator, number));
}
mySet.union(set2);
mySet.intersection(set2);
}
}