public class Testcase {
int x1[], x2[];
Testcase(int a[], int b[]) {
x1 = a;
x2 = b;
}
Testcase(Testcase d) {
x1 = d.x1;
x2 = d.x2;
}
public void Jiaoji(Testcase a) {
for (int i = 0; i < a.x1.length; i++) {
for (int j = 0; j < a.x2.length; j++) {
if (a.x1[i] == a.x2[j]) {
System.out.print(a.x1[i] + ",");
}
}
}
System.out.println("");
}
public static void main(String[] args) {
int x1[] = {
1, 4, 6, 9, 12, 18, 19, 45
};
int x2[] = {
4, 7, 9, 13, 19, 23, 29, 67
};
Testcase b = new Testcase(x1, x2);
System.out.println("交集是;");
b.Jiaoji(b);
System.out.println("并集是;");
int union[] = union(b);
for (int i : union) {
System.out.print(i + " ");
}
System.out.print("\n差集是: ");
System.out.print("\n");
int diff[] = difference(b);
for (int i : diff) {
System.out.print(i + " ");
}
}
//并集
static public int[] union(Testcase a) {
HashSetset = new HashSet ();
for (int i : a.x1) {
set.add(new Integer(i));
}
for (int i : a.x2) {
set.add(new Integer(i));
}
int size = set.size();
int out[] = new int[size];
Iteratoriter = set.iterator();
for (int i = 0; i < size; i++) {
//
//while(i.hasNext()){
out[i] = ((Integer) iter.next()).intValue();
}
return out;
}
//差集
static public int[] difference(Testcase a) {
HashSetset = new HashSet ();
for (int i : a.x1) {
set.add(new Integer(i));
}
for (int i : a.x2) {
set.remove(new Integer(i));
}
int size = set.size();
int out[] = new int[size];
Iteratoriter = set.iterator();
for (int i = 0; i < size; i++) {
out[i] = ((Integer) iter.next()).intValue();
}
return out;
}
}
参数传个数组不就行了么 或者链表 吧结果存到链表上