有两个数组:
String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
String[] arr02={"Andy","Bill","Felex","Green","Gates"};
求存在于arr01而不存在于arr02的元素的集合?
最容易想到的解法-双重循环
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 利用双重循环实现的筛选
- */
- public class DoubleCycling{
- public static void main(String[] args){
- String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
- String[] arr02={"Andy","Bill","Felex","Green","Gates"};
- // 筛选过程,注意其中异常的用途
- List<String> ls=new ArrayList<String>();
- for(String str:arr01){
- try{
- ls.add(getNotExistStr(str,arr02));
- }
- catch(Exception ex){
- continue;
- }
- }
- // 取得结果
- Object[] arr03=ls.toArray();
- for(Object str:arr03){
- System.out.println(str);
- }
- }
- /**
- * 查找数组Arr中是否包含str,若包含抛出异常,否则将str返回
- * @param str
- * @param arr
- * @return
- * @throws Exception
- */
- public static String getNotExistStr(String str,String[] arr) throws Exception{
- for(String temp:arr){
- if(temp.equals(str)){
- throw new Exception("");
- }
- }
- return str;
- }
- }
import java.util.ArrayList;
import java.util.List;
/**
* 利用双重循环实现的筛选
*/
public class DoubleCycling{
public static void main(String[] args){
String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
String[] arr02={"Andy","Bill","Felex","Green","Gates"};
// 筛选过程,注意其中异常的用途
List<String> ls=new ArrayList<String>();
for(String str:arr01){
try{
ls.add(getNotExistStr(str,arr02));
}
catch(Exception ex){
continue;
}
}
// 取得结果
Object[] arr03=ls.toArray();
for(Object str:arr03){
System.out.println(str);
}
}
/**
* 查找数组Arr中是否包含str,若包含抛出异常,否则将str返回
* @param str
* @param arr
* @return
* @throws Exception
*/
public static String getNotExistStr(String str,String[] arr) throws Exception{
for(String temp:arr){
if(temp.equals(str)){
throw new Exception("");
}
}
return str;
}
}
速度较高的解法-利用哈希表
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Hashtable;
- import java.util.List;
- import java.util.Map;
- /**
- * 利用哈希表进行筛选
- */
- public class HashtableFilter{
- public static void main(String[] args){
- String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
- String[] arr02={"Andy","Bill","Felex","Green","Gates"};
- Map<String,String> ht=new Hashtable<String,String>();
- // 將arr02所有元素放入ht
- for(String str:arr02){
- ht.put(str, str);
- }
- // 取得在ht中不存在的arr01中的元素
- List<String> ls=new ArrayList<String>();
- for(String str:arr01){
- if(ht.containsKey(str)==false){
- ls.add(str);
- }
- }
- // 取得结果
- Object[] arr03=ls.toArray();
- for(Object str:arr03){
- System.out.println(str);
- }
- }
- }
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
/**
* 利用哈希表进行筛选
*/
public class HashtableFilter{
public static void main(String[] args){
String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
String[] arr02={"Andy","Bill","Felex","Green","Gates"};
Map<String,String> ht=new Hashtable<String,String>();
// 將arr02所有元素放入ht
for(String str:arr02){
ht.put(str, str);
}
// 取得在ht中不存在的arr01中的元素
List<String> ls=new ArrayList<String>();
for(String str:arr01){
if(ht.containsKey(str)==false){
ls.add(str);
}
}
// 取得结果
Object[] arr03=ls.toArray();
for(Object str:arr03){
System.out.println(str);
}
}
}
最方便的解法-利用工具类
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 使用工具类的筛选去除
- */
- public class Tool{
- public static void main(String[] args){
- String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
- String[] arr02={"Andy","Bill","Felex","Green","Gates"};
- // 直接转的话,生成的List不支持removeAll
- List<String> ls01=new ArrayList<String>();
- for(String str:arr01){
- ls01.add(str);
- }
- // 同上
- List<String> ls02=new ArrayList<String>();
- for(String str:arr02){
- ls02.add(str);
- }
- // 去除arr01中存在于arr02中的元素
- ls01.removeAll(ls02);
- // 取得结果
- Object[] arr03=ls01.toArray();
- for(Object str:arr03){
- System.out.println(str);
- }
- }
- }
import java.util.ArrayList;
import java.util.List;
/**
* 使用工具类的筛选去除
*/
public class Tool{
public static void main(String[] args){
String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
String[] arr02={"Andy","Bill","Felex","Green","Gates"};
// 直接转的话,生成的List不支持removeAll
List<String> ls01=new ArrayList<String>();
for(String str:arr01){
ls01.add(str);
}
// 同上
List<String> ls02=new ArrayList<String>();
for(String str:arr02){
ls02.add(str);
}
// 去除arr01中存在于arr02中的元素
ls01.removeAll(ls02);
// 取得结果
Object[] arr03=ls01.toArray();
for(Object str:arr03){
System.out.println(str);
}
}
}
利用二叉树的解法
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 使用二叉樹的筛选去除
- */
- public class Test{
- public static void main(String[] args){
- String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
- String[] arr02={"Andy","Bill","Felex","Green","Gates"};
- // 以數組2為基礎創建二叉樹
- Tree tree=new Tree();
- for(String str:arr02){
- tree.insert(str);
- }
- // 將在二叉樹中不存在的元素放入鏈錶
- List<String> ls=new ArrayList<String>();
- for(String str:arr01){
- if(tree.find(str)==null){
- ls.add(str);
- }
- }
- // 輸出
- for(String str:ls){
- System.out.println(str);
- }
- }
- }
import java.util.ArrayList;
import java.util.List;
/**
* 使用二叉樹的筛选去除
*/
public class Test{
public static void main(String[] args){
String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
String[] arr02={"Andy","Bill","Felex","Green","Gates"};
// 以數組2為基礎創建二叉樹
Tree tree=new Tree();
for(String str:arr02){
tree.insert(str);
}
// 將在二叉樹中不存在的元素放入鏈錶
List<String> ls=new ArrayList<String>();
for(String str:arr01){
if(tree.find(str)==null){
ls.add(str);
}
}
// 輸出
for(String str:ls){
System.out.println(str);
}
}
}