刪除和標記重複我在這個在一個字符串數組
String array[] = {"test","testing again", "test"};
,我想,以紀念和刪除重複。這裏是我需要的輸出:
2x test
testing again
有人可以幫我做這個嗎? 我已經嘗試過Set,但它似乎沒有識別字符串已經在內部。
這裏是我的代碼:
Set addons = new HashSet<String>();
final String[] arr ={"test","testing again", "test"};
for (int i = 0; i < arr.length; i++) {
Log.d(TAG, "contains adding " + arr[i]);
if (addons.contains(arr[i])) {
//never enters here
Log.d(TAG, "contains " + arr[i]);
addons.remove(arr[i]);
addons.add("2 x " + arr[i]);
} else {
addons.add("1 x " + arr[i]);
}
}
嘗試使用HashMap中 – agad
@agad可以提供答案如果它有效,請使用代碼示例? –
使用調試器也是個不錯的主意(請參閱你在'if語句中設置的內容)。 – agad