检查所有数组值是不同的

时间:2022-12-08 07:59:20

so i have 4 arraylist values and i want to see that they all diffrents so there will not be an duplicate value(all values are strings) Here is my code hope you can help me with that i am kind of confusse i tried look up for that i began to mased up with to many if conditions for each value.

所以我有4个arraylist值,我想看到他们都是diffrents所以不会有重复的值(所有值都是字符串)这是我的代码希望你可以帮助我,我有点confusse我试着查找如果每个价值的条件,我开始与许多人交往。

    List<String>Answers=new ArrayList<String>();//מערך תשובות
    Answers.add(f.getName().toString());//הוספת התשובה הנכונה
    num = r.nextInt(30);
    //if(db.getFlag(num).getName().toString()!= f.getName().toString())
    //{
    Answers.add(db.getFlag(num).getName().toString());//הוספת 3 תשובות רנדומליות
    num = r.nextInt(30);
//  }
//  else
//      num = r.nextInt(30);        
    Answers.add(db.getFlag(num).getName().toString());
    num = r.nextInt(30);
    Answers.add(db.getFlag(num).getName().toString());

3 个解决方案

#1


4  

You could check if the List already contains your value:

您可以检查列表是否已包含您的值:

String valToAdd = db.getFlag(num).getName().toString();
if(!Answers.contains(valToAdd)){
    Answers.add(valToAdd);
}

Also, from your comments, I see you try to compare Strings with == or !=.

另外,根据您的评论,我看到您尝试将字符串与==或!=进行比较。

Always use string1.equals(string2) for comparement for Strings, as they are objects, and not primitive datatypes.

始终使用string1.equals(string2)进行字符串比较,因为它们是对象,而不是原始数据类型。

#2


2  

if you don't want duplicates then opt of Set Interface

如果你不想要重复,那么选择Set Interface

Set<String> namesSet=new HashSet<>();
namesSet.add("ABC");
namesSet.add("DEF");
namesSet.add("ABC"); // this will not be added because Set doesn't allow any duplicates

Note : Set#add() method returns boolean indicating that whether or not element added successfully

注意:Set#add()方法返回boolean,指示元素是否成功添加

#3


1  

Use HashSet instead of ArrayList. Hash set ensures that you dont have duplicate values. Just use in the same way as you are implementing ArrayList. 
e.g.
    HashSet<String> answers=new HashSet<String>();  
    answers.add(f.getName().toString());
    num = r.nextInt(30);

    answers.add(db.getFlag(num).getName().toString());
    num = r.nextInt(30);

    answers.add(db.getFlag(num).getName().toString());
    num = r.nextInt(30);
    answers.add(db.getFlag(num).getName().toString()); 

#1


4  

You could check if the List already contains your value:

您可以检查列表是否已包含您的值:

String valToAdd = db.getFlag(num).getName().toString();
if(!Answers.contains(valToAdd)){
    Answers.add(valToAdd);
}

Also, from your comments, I see you try to compare Strings with == or !=.

另外,根据您的评论,我看到您尝试将字符串与==或!=进行比较。

Always use string1.equals(string2) for comparement for Strings, as they are objects, and not primitive datatypes.

始终使用string1.equals(string2)进行字符串比较,因为它们是对象,而不是原始数据类型。

#2


2  

if you don't want duplicates then opt of Set Interface

如果你不想要重复,那么选择Set Interface

Set<String> namesSet=new HashSet<>();
namesSet.add("ABC");
namesSet.add("DEF");
namesSet.add("ABC"); // this will not be added because Set doesn't allow any duplicates

Note : Set#add() method returns boolean indicating that whether or not element added successfully

注意:Set#add()方法返回boolean,指示元素是否成功添加

#3


1  

Use HashSet instead of ArrayList. Hash set ensures that you dont have duplicate values. Just use in the same way as you are implementing ArrayList. 
e.g.
    HashSet<String> answers=new HashSet<String>();  
    answers.add(f.getName().toString());
    num = r.nextInt(30);

    answers.add(db.getFlag(num).getName().toString());
    num = r.nextInt(30);

    answers.add(db.getFlag(num).getName().toString());
    num = r.nextInt(30);
    answers.add(db.getFlag(num).getName().toString());