所以我試圖創建一個for循環找到ArrayList
中的獨特元素。 我已經有一個ArrayList
存儲與用戶輸入的20個地方(允許重複),但我堅持如何計算列表中輸入的不同地方的數量,不包括重複。 (我想避免使用哈希)如何找到數組列表中的唯一字數
輸入:
[park, park, sea, beach, town]
輸出:
[Number of unique places = 4]
繼承人我試圖使代碼的一個粗略的例子:
public static void main(String[] args) {
ArrayList<City> place = new ArrayList();
Scanner sc = new Scanner(System.in);
for(...) { // this is just to receive 20 inputs from users using the scanner
...
}
# This is where i am lost on creating a for loop...
}
爲什麼你要避免使用哈希集?這是迄今爲止處理這個問題最簡單也可能是最有效的方法。 –
哈哈是啊我知道,但我之前看過它,我試過了,但我真的不明白如何使用哈希非常清楚。所以我要使用它並登上腳本,我懷疑我可以解釋清楚它的工作原理。 – brand