首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java:在数组中查找重复项

Java:在数组中查找重复项
EN

Stack Overflow用户
提问于 2018-10-23 00:42:00
回答 2查看 74关注 0票数 -8

我需要在我的程序中实现一个重复的搜索方法,但是得到了错误。如果我注释掉重复搜索部分,程序运行正常。

错误(我注释掉了重复的搜索部件):

代码语言:javascript
复制
  do{ 
   System.out.print("Please enter an integer or -1 to stop: ");
   input=Integer.parseInt(scan.nextLine()); //parse library function 
   /** 
    * for(int i=0; i<A.length; i++){
    *     for(int j=i+1; j<A.length; j++{
    *         if(A[i].equals(A[j]))
    *         System.out.println("Duplicate input. Please enter another 
                          value: ");
    *         }
    *       }
    */
   if(input != -1) //if input is 
   Display.userInput(input);
}
while(input != -1); 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-23 00:52:36

尝尝这个。

代码语言:javascript
复制
do{ 
   System.out.print("Please enter an integer or -1 to stop: ");
   input=Integer.parseInt(scan.nextLine());
   boolean flag = true;
   for(int i=0; i<A.length; i++){ 
      if(A[i].equals(input)) {
          System.out.println("Duplicate input. Please enter another value: ");
          flag = false;
          break;
      }
   }
   if(!flag) {
      continue;
   }
   if(input != -1) //if input is 
   Display.userInput(input);
}

while(input != -1); 
票数 0
EN

Stack Overflow用户

发布于 2018-10-23 00:56:45

代码语言:javascript
复制
    int A[] = {1, 5, 9, 5, 6};
    if (Arrays.stream(A).boxed()
            .collect(Collectors.groupingBy(Function.identity()))
            .entrySet()
            .stream()
            .map(Map.Entry::getValue)
            .map(Collection::size)
            .anyMatch(s -> s > 1)) {
        System.out.println("Duplicate input. Please enter another value:");
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52934032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档