现有两个以下任意格式的数字(int类型)区间组成的字符串,如10-15,
现有两个字符串A和B,完成以下两个方法:
- 求两个字符串的交集,如:"1-10"和"3-15"两个的交集为"3-10";
- 求两个字符串的合集,如: "1-10"和"3-15"两个的合集为"1-15";
现有两个以下任意格式的数字(int类型)区间组成的字符串,如10-15,
现有两个字符串A和B,完成以下两个方法:
package intersectionarrays;
import java.util.Arrays;
import java.util.HashSet;
public class IntersectionIntegerArrays {
public static void main(String[] args) {
Integer[] firstArray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Integer[] secondArray = { 1, 3, 5, 7, 9 };
Integer[] thirdArray = { 1, 5, 9 };
HashSet<Integer> set = new HashSet<>();
set.addAll(Arrays.asList(firstArray));
set.retainAll(Arrays.asList(secondArray));
set.retainAll(Arrays.asList(thirdArray));
System.out.println(set);
// convert to array
Integer[] intersection = {};
intersection = set.toArray(intersection);
System.out.println(Arrays.toString(intersection));
}
}
[1, 5, 9]
[1, 5, 9]