我想在Java中有联合,相交,差异和反向操作。
首先我有2个ArrayList< Integer>
a = [0,2,4,5,6,8,10]
b = [5,6,7,8,9,10]
一个工会b应该返回c = [0,2,3,4,5,6,7,8,9,10]
相交b应返回c = [5,8,10]
一个defference b应该返回c = [0,2,3,4]
反向a = [10,8,6,5,4,2,0]
这样的东西。
如何在Java中实现该方法?
更新:我必须从这个模板开始:
package IntSet;
import java.util.ArrayList;
import java.util.Collection;
public class IntSet {
private ArrayList intset;
public IntSet(){
intset = new ArrayList();
}
public void insert(int x){
intset.add(x);
}
public void remove(int x){
//implement here
intset.indexOf(x);
}
public boolean member(int x){
//implement here
return true;
}
public IntSet intersect(IntSet a){
//implement here
return a;
}
public IntSet union(IntSet a){
//implement here
return a;
}
public IntSet difference(IntSet a){
//implement here
IntSet b = new IntSet();
return b;
}