public static void main(String[] args){
String[] a = new String[]{"1","5","3","7"};
String[] b = new String[]{"2","3","4"};
String[] arrResult = arrContrast(a, b);
for (String strResult : arrResult) {
System.out.println("最后的結果:----------->" + strResult);
}
}
注:輸出結果 最后的結果:----------->1
最后的結果:----------->5
最后的結果:----------->7
//處理數組字符
private static String[] arrContrast(String[] arr1, String[] arr2){
List<String> list = new LinkedList<String>();
for (String str : arr1) { //處理第一個數組,list里面的值為1,2,3,4
if (!list.contains(str)) {
list.add(str);
}
}
for (String str : arr2) { //如果第二個數組存在和第一個數組相同的值,就刪除
if(list.contains(str)){
list.remove(str);
}
}
String[] result = {}; //創建空數組
return list.toArray(result); //List to Array
}
新特性 ::::
public static void main(String[] args) {
String[] aa = { "1", "2", "3" };
for (String arr : aa) {
System.out.println(arr); // java1新特性嘛。遍歷數組、list嘛!
}
for (int i = 0; i < aa.length; i++) {
System.out.println(aa[i]);
}
}
輸出結果: 1
2
3
1
2
3