2017-01-13 41 views
0

所以我試圖創建一個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... 

} 
+10

爲什麼你要避免使用哈希集?這是迄今爲止處理這個問題最簡單也可能是最有效的方法。 –

+0

哈哈是啊我知道,但我之前看過它,我試過了,但我真的不明白如何使用哈希非常清楚。所以我要使用它並登上腳本,我懷疑我可以解釋清楚它的工作原理。 – brand

回答

1

想到一種方法(不使用Set或散列值)是做第二個列表。

ArrayList<City> places = new ArrayList<>(); 
//Fill array 

ArrayList<String> uniquePlaces = new ArrayList<>(); 
for (City city : places){ 
    if (!uniquePlaces.contains(city.getPlace())){ 
     uniquePlaces.add(city.getPlace()); 
    } 
} 

//number of unique places: 
int uniqueCount = uniquePlaces.size(); 

注意,這是不是超級效率= d

+0

好的,這是我想念第二個列表感謝!現在我可以使用if條件的嵌套for循環比較2列表。 – brand

+0

@brand - 不,不要使用嵌套循環。只需使用List的'contains()'方法;它已經爲你編碼,並用一個很好的方法打包。 –

5

你可以使用Set。 https://docs.oracle.com/javase/7/docs/api/java/util/Set.html

將列表數據存儲到SetSet將不會有重複,所以set的大小將是沒有重複的元素。

使用此方法獲取設置大小。 https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#size()

示例代碼。

 List<String> citiesWithDuplicates = 
       Arrays.asList(new String[] {"park", "park", "sea", "beach", "town"}); 
     Set<String> cities = new HashSet<>(citiesWithDuplicates); 

     System.out.println("Number of unique places = " + cities.size()); 
+0

也許地圖是這個問題的最佳結構,但即使如此,如果您想回答,至少應該提供一些實施細節。 –

+0

@TimBiegeleisen - 爲什麼地圖會比一套更好? –

+0

@TedHopp那你爲什麼建議在上面使用'HashSet'? –

1

如果你不想使用SetMap接口的實現(這將解決你問題的一行代碼),並要套牢ArrayList,我建議使用類似的方法Collections.sort()。它會分類你的元素。然後遍歷排序的數組並比較和計數重複項。這個技巧可以使解決迭代問題更容易。

無論如何,我強烈推薦使用Set接口的實現之一。

2

如果你能夠使用Java 8,你可以使用Java的distinct法流:

int numOfUniquePlaces = list.stream().distinct().count(); 

否則,使用set是最簡單的解決方案。既然你不想使用「散列」,使用TreeSet(儘管HashSet在大多數情況下是更好的解決方案)。如果這不是一個選項,你將不得不手動檢查每個元素是否重複。

0

使用以下答案。如果有多個重複元素,這將在不同列表中添加最後一個重複元素。

List<String> citiesWithDuplicates = Arrays.asList(new String[] { 
       "park", "park", "sea", "beach", "town", "park", "beach" }); 
     List<String> distinctCities = new ArrayList<String>(); 

     int currentIndex = 0; 

     for (String city : citiesWithDuplicates) { 
      int index = citiesWithDuplicates.lastIndexOf(city); 
      if (index == currentIndex) { 
       distinctCities.add(city); 
      } 
      currentIndex++; 
     } 
      System.out.println("[ Number of unique places = " 
      + distinctCities.size() + "]"); 
0

那麼如果你不想使用任何HashSets或類似的選項,一個快速和骯髒的嵌套for循環像這樣的例子並招(它,如果你有很多的項目是地獄只是緩慢(20會很好)):

int differentCount=0; 
for(City city1 : place){ 
    boolean same=false; 
    for(City city2 : place){ 
     if(city1.equals(city2)){ 
     same=true; 
     break; 
     } 
    } 
    if(!same) 
     differentCount++; 
} 
System.out.printf("Number of unique places = %d\n",differentCount); 
+1

是的它有點慢,但它解決了創建第二個列表的想法和它工作的嵌套循環的問題 – brand

相關問題