java 集合差运算_用一个参数的JAVA程序实现集合的交并差运算
展开全部
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
for (int j = 0; 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("并集32313133353236313431303231363533e78988e69d8331333335313733是;");
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) {
HashSet set = 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];
Iterator iter = set.iterator();
for (int i = 0; i
//
//while(i.hasNext()){
out[i] = ((Integer) iter.next()).intValue();
}
return out;
}
//差集
static public int[] difference(Testcase a) {
HashSet set = 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];
Iterator iter = set.iterator();
for (int i = 0; i
out[i] = ((Integer) iter.next()).intValue();
}
return out;
}
}
总结
以上是生活随笔为你收集整理的java 集合差运算_用一个参数的JAVA程序实现集合的交并差运算的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: ubuntu安装mysql 密码忘了怎么
- 下一篇: java 按键框架,如何在java中更改