【问题标题】:More than once in an arrayList with user input在具有用户输入的 arrayList 中不止一次
【发布时间】:2016-05-05 07:08:49
【问题描述】:

在下面的代码中,我需要检查一个数字是否在ArrayList 中出现多次。用户输入一个号码进行咨询,如果在array中存在且有多个消息,则“出现多次显示”。我一直在尝试这样做,但我不知道该怎么做。

这是main 类:

    ArrayList<Integer> list = new ArrayList<>();
    list.add(3);
    list.add(2);
    list.add(7);
    list.add(2);

    System.out.println("Type a number: ");
    int number = Integer.parseInt(sc.nextLine());
    if (Metodos.moreThanOne(list, number)) {
        System.out.println(number + " appears more than once.");
    } else {
        System.out.println(number + " does not appear more than once.");
    }

这是方法:

public static boolean moreThanOne(ArrayList<Integer> list, int number) {
    for (Integer in : list) {
        if (list.contains(number) && in==in) {
            return true;
        }
    }
    return false;
}

【问题讨论】:

    标签: java arraylist data-structures


    【解决方案1】:

    您可以使用此代码:

    if (list.lastIndexOf(element) != list.indexOf(element)) {
      return true; // you have at least two numbers
    } else {
      return false; // element is not exist or you have only one element
    }
    

    【讨论】:

    • 为什么不直接这个? return list.lastIndexOf(element) != list.indexOf(element);
    • @RolandIllig 因为如果请求的元素不存在,您将从左侧和右侧获得 -1
    • 但是你的代码做的完全一样,只是多了几行。
    • @RolandIllig 对不起,是一样的
    • 哦不,这又不是if true return true else false 模式。
    【解决方案2】:
        public static boolean moreThanOne(ArrayList<Integer> list, int number) 
        {
          int count = 0;
          for (Integer in : list) {
              if (number == in) {
                  count++;
              }
          }
          if(count > 1)
             return true;
          else 
             return false;
          }
        }
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

      public class StartHere {
      public static void main(String[] args) {
      
          ArrayList<Integer> list = new ArrayList<>();
          list.add(3);
          list.add(2);
          list.add(7);
          list.add(2);
      
          Scanner scanner = new Scanner(System.in);
      
          System.out.print("Type a number: ");
          String input = scanner.nextLine();
          int number = Integer.parseInt(input);
          scanner.close();
          if(moreThanOnce(list, number)){
              System.out.println("The number appears more than once.");
          }else{
              System.out.println("The number doesn't appear more than once.");
          }
      
      }
      
      public static boolean moreThanOnce(ArrayList<Integer> list, int number) {
          boolean bool = false;
          int counter = 0;
          for (Integer value : list) {
              if (value == number) {
                  counter++;
              }
          }
          if (counter > 1){
              bool = true;
          }
          return bool;
      }
      }
      

      提示:您应该提高英语水平,以便更容易理解您的问题。一个很好的方法是阅读用这种语言写的书。

      【讨论】:

      • 我试过了,不行,如果你输入3或7,会提示“该数字出现多次”。显示,您只是检查该号码是否在列表中。
      • 哦哦...等一下,我会解决的。
      • 与 Alikbar 发布的答案相同,谢谢
      【解决方案4】:
          System.out.println("Type a number: ");
          Set<Integer> set = new HashSet<Integer>(list);
          Scanner scanner = new Scanner(System.in);
          int number = Integer.parseInt(scanner.nextLine());
      
          if (!set.add(number)) {
              System.out.println(number + " appears more than once.");
          } else {
              System.out.println(number + " does not appear more than once.");
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-07
        • 1970-01-01
        • 2021-12-14
        • 2018-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-17
        相关资源
        最近更新 更多