鑑於這兩個數組作爲一個例子:
String[] arr1 = new String[] {"A", "B", "C", "D", "E"};
String[] arr2 = new String[] {"A", "C", "D"};
好,只用數組,我會做這樣的事情:
首先,一個方法,看看一個數組包含一些值:
public boolean aContainsB(String[] a, String b) {
for (String s : a) if (s.equals(b)) return true;
return false;
}
然後,創建一個與最大數組一樣大的新數組。它將保存不在數組中的值。它可能是每個字母,我假設。
String[] notFound = new String[Math.max(arr1.length, arr2.length)];
然後,循環遍歷第一個數組,如果當前值不在第二個數組中,則將其追加到未找到的數組。
int i = 0;
for (String s : arr1) if (!aContainsB(arr2, s)) notFound[i++] = s;
在本月底,我會包含多少值不存在第二陣列中,你可以通過NOTFOUND陣列循環我次打印出來。
System.out.println("There are " + i + " elements in arr1 that are not in arr2, they are:");
for (int j = 0; j < i; j++) {
System.out.println("The String: " + notFound[j]);
}
輸出將是:
There are 2 elements in arr1 that are not in arr2, they are:
The String: B
The String: E
請加上這是指(Java的?)作爲標記語言。 – 2014-09-28 21:10:12
對不起。我只是添加它。爲您的建議thnks。 – 2014-09-28 21:12:21
我還不確定你的問題是什麼。你想用數組做什麼? – 2014-09-28 21:13:20