配列は集合の並列,交差,差を実現する
2529 ワード
/**
* , , ,
* @author BreAthe
*
*/
class SetOperator {
/**
* setOne,setTwo
* @param setOne
* @param setTwo
*/
public void intersection(int[] setOne, int[] setTwo) {
for(int i = 0; i < setTwo.length; i++) {// setTwo
for(int j = 0; j < setOne.length; j++) {
if(setOne[j] == setTwo[i]) {
System.out.print(setOne[j] + " ");
break;
}
}
}
}
/**
* setOne,setTwo
* setOne - setTwo
* @param setOne
* @param setTwo
*/
public void difference(int[] setOne, int[] setTwo) {
for(int i = 0; i < setTwo.length; i++) {
boolean flag = false;
for(int j = 0; j < setOne.length; j++) {
if(setOne[j] == setTwo[i]) {
flag = true;
break;
}
}
if(!flag) {
System.out.print(setTwo[i] + " ");
}
}
}
/**
* setOne,setTwo
* @param setOne
* @param setTwo
*/
public void union(int[] setOne, int[] setTwo) {
for(int i = 0; i < setOne.length; i++) {
System.out.print(setOne[i] + " ");
}
for(int i = 0; i < setTwo.length; i++) {// setTwo
boolean flag = false;
for(int j = 0; j < setOne.length; j++) {
if(setTwo[i] == setOne[j]) {
flag = true;
break;
}
}
if(!flag) {
System.out.print(setTwo[i] + " ");
}
}
}
public void prset(int[] set) {
for(int i = 0; i < set.length; i++) {
System.out.print(set[i] + " ");
}
System.out.println();
}
public class SetOperatorTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] one = {2, 5, 7, 8, 10, 12, 15, 17, 20};
int[] two = {3, 5, 7, 9, 13};
SetOperator so = new SetOperator();
System.out.println(" 1 :");
so.prset(one);
System.out.println(" 2 :");
so.prset(two);
System.out.println(" :");
so.union(one, two);
System.out.println("
:");
so.intersection(one, two);
System.out.println("
1 2 :");
so.difference(one, two);
}
}