数组并集,交集,差集
2015-12-24 15:26
561 查看
import java.util.LinkedList;
public class ArrayTest {
/**
* @param args
*/
public static void main(String[] args) {
String[] str1={"aa","bb","cc","dd","cd"};
String[] str2={"ab","cd","bb","mm"};
System.out.println("---------------交集------------------");
String[] intersect_result=intersect(str1,str2);
for (String str : intersect_result) {
System.out.print(str+",");
}
System.out.println("");
System.out.println("---------------差集------------------");
String[] chaSet_result=chaSet(str1,str2);
for (String str : chaSet_result) {
System.out.print(str+",");
}
System.out.println("");
System.out.println("---------------并集------------------");
String[] bingSet_result=bingSet(str1,str2);
for (String str : bingSet_result) {
System.out.print(str+",");
}
}
//求两个数组的交集
public static String[] intersect(String[] arr1, String[] arr2) {
LinkedList<String> newL = new LinkedList<String>();
LinkedList<String> history = new LinkedList<String>();
String[] longerArr = arr1;
String[] shorterArr = arr2;
//找出较长的数组来减较短的数组
if (arr1.length > arr2.length) {
longerArr = arr1;
shorterArr = arr2;
}
for (String str : longerArr) {
if (!history.contains(str)) {
history.add(str);
}
}
for (String str : shorterArr) {
if (history.contains(str)) {
newL.add(str);
history.remove(str);
} else {
history.remove(str);
}
}
String[] result = {};
return newL.toArray(result);
}
//求两个数组的差集
public static String[] chaSet(String[] arr1, String[] arr2) {
LinkedList<String> newL = new LinkedList<String>();
LinkedList<String> history = new LinkedList<String>();
String[] longerArr = arr1;
String[] shorterArr = arr2;
//找出较长的数组来减较短的数组
if (arr1.length > arr2.length) {
longerArr = arr1;
shorterArr = arr2;
}
for (String str : longerArr) {
if (!history.contains(str)) {
history.add(str);
}
}
for (String str : shorterArr) {
if (history.contains(str)) {
history.remove(str);
} else {
newL.add(str);
history.remove(str);
}
}
for(String str : history){
newL.add(str);
}
String[] result = {};
return newL.toArray(result);
}
//求两个数组的并集
public static String[] bingSet(String[] arr1, String[] arr2) {
LinkedList<String> newL = new LinkedList<String>();
LinkedList<String> history = new LinkedList<String>();
String[] longerArr = arr1;
String[] shorterArr = arr2;
//找出较长的数组来减较短的数组
if (arr1.length > arr2.length) {
longerArr = arr1;
shorterArr = arr2;
}
for (String str : longerArr) {
if (!history.contains(str)) {
history.add(str);
}
}
for (String str : shorterArr) {
if (!history.contains(str)) {
history.add(str);
}
}
String[] result = {};
return history.toArray(result);
}
}
public class ArrayTest {
/**
* @param args
*/
public static void main(String[] args) {
String[] str1={"aa","bb","cc","dd","cd"};
String[] str2={"ab","cd","bb","mm"};
System.out.println("---------------交集------------------");
String[] intersect_result=intersect(str1,str2);
for (String str : intersect_result) {
System.out.print(str+",");
}
System.out.println("");
System.out.println("---------------差集------------------");
String[] chaSet_result=chaSet(str1,str2);
for (String str : chaSet_result) {
System.out.print(str+",");
}
System.out.println("");
System.out.println("---------------并集------------------");
String[] bingSet_result=bingSet(str1,str2);
for (String str : bingSet_result) {
System.out.print(str+",");
}
}
//求两个数组的交集
public static String[] intersect(String[] arr1, String[] arr2) {
LinkedList<String> newL = new LinkedList<String>();
LinkedList<String> history = new LinkedList<String>();
String[] longerArr = arr1;
String[] shorterArr = arr2;
//找出较长的数组来减较短的数组
if (arr1.length > arr2.length) {
longerArr = arr1;
shorterArr = arr2;
}
for (String str : longerArr) {
if (!history.contains(str)) {
history.add(str);
}
}
for (String str : shorterArr) {
if (history.contains(str)) {
newL.add(str);
history.remove(str);
} else {
history.remove(str);
}
}
String[] result = {};
return newL.toArray(result);
}
//求两个数组的差集
public static String[] chaSet(String[] arr1, String[] arr2) {
LinkedList<String> newL = new LinkedList<String>();
LinkedList<String> history = new LinkedList<String>();
String[] longerArr = arr1;
String[] shorterArr = arr2;
//找出较长的数组来减较短的数组
if (arr1.length > arr2.length) {
longerArr = arr1;
shorterArr = arr2;
}
for (String str : longerArr) {
if (!history.contains(str)) {
history.add(str);
}
}
for (String str : shorterArr) {
if (history.contains(str)) {
history.remove(str);
} else {
newL.add(str);
history.remove(str);
}
}
for(String str : history){
newL.add(str);
}
String[] result = {};
return newL.toArray(result);
}
//求两个数组的并集
public static String[] bingSet(String[] arr1, String[] arr2) {
LinkedList<String> newL = new LinkedList<String>();
LinkedList<String> history = new LinkedList<String>();
String[] longerArr = arr1;
String[] shorterArr = arr2;
//找出较长的数组来减较短的数组
if (arr1.length > arr2.length) {
longerArr = arr1;
shorterArr = arr2;
}
for (String str : longerArr) {
if (!history.contains(str)) {
history.add(str);
}
}
for (String str : shorterArr) {
if (!history.contains(str)) {
history.add(str);
}
}
String[] result = {};
return history.toArray(result);
}
}
相关文章推荐
- iOS 画虚线以及drawRect的使用总结:
- CRM域用户误删恢复
- 微信支付接口 H5
- 运营平台框架整理(内部用)
- 多个Tomcat实例运行于一台服务器的配置
- spark文档汇总
- 快速解决mysql Lost connection to MySQL server at 'reading initial communication packet及can't connect to mysql server on 'localhost'
- ORACLE存储过程中%TYPE和%ROWTYPE的区别
- Tutorial: Facebook analytics using Power BI Desktop
- js url 中文乱码
- 陈氏编程规范
- angularJS seed 安装
- quartz配置文件详解
- 借助FileReader实现将文件编码为Base64后通过AJAX上传
- VC++实现恢复SSDT
- 单例模式【Singleton Pattern 】
- string的用法汇总
- Hadoop HA 与 Federation
- 数据的拆箱与装箱
- linux常用命令