允许用户输入唯一值

时间:2017-09-05 10:45:48

标签: java arraylist

我试图从用户那里获得输入以从ArrayList中选择玩家。 ArrayList中的所有玩家都有一个唯一的ID,用户选择5到8个玩家来启动程序,但我不想让用户再次输入相同的ID,因为它会有重复。 继承人我想做的事情

I still am not getting this I tried this  

     public void SelectAthlete(){
       Data ath = new Data();
         ath.athleteData();
        boolean choice = true;
        int p=0;
        String id;
        int value =0;
        Scanner input = new Scanner(System.in);
        System.out.println();
        do{
            System.out.println();
            System.out.print("\tEnter the number of Participants you want to Compete: ");
            p=input.nextInt();
            System.out.println("\tYou have Decided to compete with" +" " + p + " " +"Athletes");
            if(p>=5 && p<=8){
                System.out.println("\tEnter the Athlete ID :    ");
                for (int i=0;i<p;i++){
                    value=0;
                    id = input.next();
                    if(id.substring(0,1).equals("R") || id.substring(0,1).equals("P")){ 

                        for(int k=0;i<Data.AthleteData.size();k++)
                        {
                    if(Data.AthleteData.get(k).getID().contains(id))
                        {
                        value++;
                        choice = Data.Inputlist.add(id);
                        }
                    else if (!choice)
                    {
                        value--;
                        System.out.println("Please Enter Unique Value");
                        input.nextLine();
                    }                       

                        for(int j = 0; j<Data.AthleteData.size();j++)
                        {
                            if(id.equals(Data.AthleteData.get(j).getID()))
                            {
                                value++;
                            }
                        }
                    }
                    }
                    if(value!=0)
                    {
                        Data.Inputlist.add(id);
                    }
                    else
                    {
                        System.out.println("Enter a valid ID (in UPPER Case)");
                        input.nextLine();
                        i--;
                    }

                }
                choice = false;

            }
                        else{
                            System.out.println("\n\tHowever You need to have 
       atleast 5 Athletes or atmost 8 Athletes to Start a game.");
      input.nextLine();
      } 
            for (int m=0;m<Data.AthleteData.size();m++){                    
               if (Data.Inputlist.contains(Data.AthleteData.get(m).getID()))
       {                        
         System.out.println(Data.AthleteData.get(m));
                            }
                        }
        }while(choice);

2 个答案:

答案 0 :(得分:1)

您可以使用HashSet并查看add方法的响应,如下所示:

Set<String> someSet = new HashSet<String>();
boolean isUnique = someSet.add("abc");
System.out.println(isUnique); // this will output true as abc does not already exist in the set and add operation was successful            
isUnique = someSet.add("abc");
System.out.println(isUnique); //this will output false as abc already exists in the set and hence cannot be added again        

但是由于您使用的是自定义对象而不是字符串,因此您需要按如下方式声明该集:

Set<YourClass> someSet = new HashSet<YourClass>();

除此之外,您还需要确保YourObject类中的equals方法也正确实现。 您可以参考这些链接以获取有关equals的更多信息:http://www.geeksforgeeks.org/overriding-equals-method-in-java/https://www.mkyong.com/java/java-how-to-overrides-equals-and-hashcode/

答案 1 :(得分:0)

有很多方法可以做到这一点 1.)在获取数字后,您可以循环返回以检查它是否存在于Arraylist中。(不推荐),因为它具有较高的运行时复杂度。
2.)只需使用arraylist的contains()方法检查它是否已存在 3)您可以使用bitset作为数字,并在分配时将其设置为true。后来您可以将其与bool值进行比较以检查它是否设置。有关bitset的更多信息,请参阅https://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html