2017-01-13 39 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

相关问题