Java 不允许在数组列表中重复对象

Java 不允许在数组列表中重复对象,java,arraylist,Java,Arraylist,我目前正在用Java开发一款家庭世仇游戏。我有一个名为questions的对象问题数组列表,其中存储了在整个游戏中使用的随机问题。我正在尝试填充数组列表,以确保没有重复项,但是它不起作用,我得到了重复项 初始化为字段的是我的问题数组列表: private ArrayList<Question> questions = new ArrayList<>(); 我确实在我的问题类中重写了equals方法,但是它没有起作用。这是我的问题课: /** * Question pr

我目前正在用Java开发一款家庭世仇游戏。我有一个名为questions的对象问题数组列表,其中存储了在整个游戏中使用的随机问题。我正在尝试填充数组列表,以确保没有重复项,但是它不起作用,我得到了重复项

初始化为字段的是我的问题数组列表:

private ArrayList<Question> questions = new ArrayList<>();
我确实在我的问题类中重写了equals方法,但是它没有起作用。这是我的问题课:

/**
 * Question proposed to players to guess one of the answers on the board
 *
 * Each question has 5 possible answers
 *
 * The top answer to each question is stored at Answers(0) and is worth the most
 * The last answer is stored at Answers(4) and is worth the least
 *
 *
 * @author Stefan Gligorevic
 */
import java.util.ArrayList;
import java.util.Random;
import java.lang.String;
import java.lang.Object;

public class Question {

    private ArrayList<Answer> answers;
    private String question;

    /**** MIGHT HAVE TO INITIALIZE ANSWERS TO AN ARRAY LIST WITH NO SIZE SO ADD CAN WORK ****/
    //constructors
    public Question(){
        answers = new ArrayList<>();
        question=getRandomQuestion();
    }

    //makes a random question with a set of answers
    public String getRandomQuestion() {
        String ask = "";

        Random rand = new Random();
        int n = 1 + rand.nextInt(10);

        switch (n)
        {
            case 1:
                ask="Name a place where a child might get seperated from its parents:";
                answers.add(new Answer("Mall", 38));
                answers.add(new Answer("Park", 23));
                answers.add(new Answer("Zoo", 16));
                answers.add(new Answer("Theme Park", 16));
                answers.add(new Answer("Airport", 5));
                break;
            case 2:
                ask="Name something for which you need a warranty:";
                answers.add(new Answer("Car", 54));
                answers.add(new Answer("TV", 23));
                answers.add(new Answer("Watch", 8));
                answers.add(new Answer("Computers", 4));
                answers.add(new Answer("Appliance", 3));
                break;
            case 3:
                ask="Name a fruit you can buy dried:";
                answers.add(new Answer("Grape", 22));
                answers.add(new Answer("Banana", 21));
                answers.add(new Answer("Apricot", 21));
                answers.add(new Answer("Prune", 17));
                answers.add(new Answer("Apple", 15));
                break;
            case 4:
                ask="Name an activity that could be rained out:";
                answers.add(new Answer("Sports Event", 45));
                answers.add(new Answer("Picnic", 34));
                answers.add(new Answer("Wedding", 10));
                answers.add(new Answer("Concert", 7));
                answers.add(new Answer("Barbecue", 3));
                break;
            case 5:
                ask="What accent might an American pretend to have in order to sound more attractive:";
                answers.add(new Answer("French", 61));
                answers.add(new Answer("British", 18));
                answers.add(new Answer("Italian", 8));
                answers.add(new Answer("Spanish", 8));
                answers.add(new Answer("Australian", 3));
                break;
            case 6:
                ask="Name a sport that might be played at a family reunion:";
                answers.add(new Answer("Football", 54));
                answers.add(new Answer("Baseball", 21));
                answers.add(new Answer("Horseshoe", 8));
                answers.add(new Answer("Frisbee", 7));
                answers.add(new Answer("Basketball", 6));
                break;
            case 7:
                ask="Name a game that would be inappropriate at a company party:";
                answers.add(new Answer("Spin the Bottle", 41));
                answers.add(new Answer("Strip Poker", 32));
                answers.add(new Answer("Twister", 11));
                answers.add(new Answer("Truth or Dare", 11));
                answers.add(new Answer("Beer Pong", 3));
                break;
            case 8:
                ask="Name something that would get you thrown out of most bars:";
                answers.add(new Answer("Getting in a fight", 45));
                answers.add(new Answer("Drinking too much", 29));
                answers.add(new Answer("Not Paying", 6));
                answers.add(new Answer("Stripping", 5));
                answers.add(new Answer("Being underage", 3));
                break;
            case 9:
                ask="Name something you would see on the Jerry Springer show:";
                answers.add(new Answer("Fighting", 56));
                answers.add(new Answer("Nudity", 22));
                answers.add(new Answer("Security", 6));
                answers.add(new Answer("Jerry Springer", 4));
                answers.add(new Answer("Chairs Thrown", 3));
                break;
            case 10:
                ask="Name a breed of dog that might be used as a guard dog:";
                answers.add(new Answer("German Shepard", 36));
                answers.add(new Answer("Pit Bull", 23));
                answers.add(new Answer("Doberman Pinscher", 20));
                answers.add(new Answer("Rottweiler", 8));
                answers.add(new Answer("Bulldog", 5));
                break;
            default:
                //won't reach this hehe
        } //end switch
        return ask;
    }

    @Override
    public boolean equals(Object obj)
    {
        if(obj == null)
            return false;

        if(!Question.class.isAssignableFrom(obj.getClass()))
            return false;

        final Question q = (Question) obj;

        if(q.getQuestion() == null || this.getQuestion() == null)
            return false;

        if(!this.getQuestion().equalsIgnoreCase(q.getQuestion()))
            return false;

        return true;
    }

    //getters and setters
    public String getQuestion() { return question; }
    public ArrayList<Answer> getAnswers() { return answers; }

    //returns answer at given index
    public Answer answerAt(int index) { return answers.get(index); }
    //returns String of answer at specified index
    public String getAnswerAt(int index) { return answers.get(index).getAnswer(); }
    //returns point value of answer at specified index
    public int getAnswerPoints(int index) { return answers.get(index).getValue(); }

    //returns the points earned for this question
    //points are the point values of all the answers that have been found
    public int pointsEarned() {
        int points=0;
        for (int i=0; i<answers.size(); i++)
        {
            if(answers.get(i).isFound())
                points += answers.get(i).getValue();
        }
        return points;
    }

    //Sets a particular answer's value
    public void setAnswerVal(int index, int val) { answers.get(index).setValue(val); }

    //Add a multiplier to each answer's value for the question
    public void addMultiplier(int multiplier)
    {
        for(int i=0; i<answers.size(); i++)
        {
            int val = answers.get(i).getValue();
            answers.get(i).setValue(multiplier * val);
        }
    }

    //returns string of each answer
    public String topAnswer() { return answers.get(0).getAnswer(); }
    public String Answer2() { return answers.get(1).getAnswer(); }
    public String Answer3() { return answers.get(2).getAnswer(); }
    public String Answer4() { return answers.get(3).getAnswer(); }
    public String lastAnswer() { return answers.get(4).getAnswer(); }

    //returns values of each answer
    public int topAnswerVal() { return answers.get(0).getValue(); }
    public int Answer2Val() { return answers.get(1).getValue(); }
    public int Answer3Val() { return answers.get(2).getValue(); }
    public int Answer4Val() { return answers.get(3).getValue(); }
    public int lastAnswerVal() { return answers.get(4).getValue(); }

}
/**
*问题建议玩家猜棋盘上的答案之一
*
*每个问题有5个可能的答案
*
*每个问题的顶部答案存储在答案(0)中,并且最有价值
*最后一个答案存储在答案(4)中,值最少
*
*
*@作者Stefan Gligorevic
*/
导入java.util.ArrayList;
导入java.util.Random;
导入java.lang.String;
导入java.lang.Object;
公开课问题{
私人ArrayList回答;
私有字符串问题;
/****可能必须初始化没有大小的数组列表的答案,以便“添加”可以工作****/
//建设者
公众问题({
answers=newarraylist();
question=getRandomQuestion();
}
//用一组答案随机提出一个问题
公共字符串getRandomQuestion(){
字符串ask=“”;
Random rand=新的Random();
整数n=1+下一次随机数(10);
开关(n)
{
案例1:
ask=“说出一个孩子可能与父母分离的地方:”;
答案。添加(新答案(“Mall”,38));
答案。添加(新答案(“公园”,23));
添加(新答案(“动物园”,16));
增加(新答案(“主题公园”,16));
答案。添加(新答案(“机场”,5));
打破
案例2:
ask=“说出你需要保修的东西:”;
答案。添加(新答案(“Car”,54));
增加(新答案(“电视”,23));
答案。添加(新答案(“观察”,8));
添加(新答案(“计算机”,4));
添加(新答案(“设备”,3));
打破
案例3:
问=“说出一种你可以买到的干果:”;
添加(新答案(“葡萄”,22));
添加(新答案(“香蕉”,21));
添加(新答案(“杏”,21));
添加(新答案(“删减”,17));
添加(新答案(“苹果”,15));
打破
案例4:
ask=“说出一项可能会被取消的活动:”;
增加(新答案(“体育赛事”,45));
添加(新答案(“野餐”,34));
添加(新答案(“婚礼”,10));
添加(新答案(“音乐会”,7));
添加(新答案(“烧烤”,3));
打破
案例5:
ask=“美国人为了听起来更有吸引力,可能会假装有什么口音:”;
增加(新的答复(“法文”,61));
添加(新答案(“英国”,18));
添加(新答案(“意大利语”,8));
增加(新的答复(“西班牙文”,8));
添加(新答案(“澳大利亚”,3));
打破
案例6:
ask=“说出一项可能在家庭聚会上进行的运动:”;
添加(新答案(“足球”,54));
添加(新答案(“棒球”,21));
添加(新答案(“马蹄铁”,8));
添加(新答案(“飞盘”,7));
添加(新答案(“篮球”,6));
打破
案例7:
ask=“说出一个在公司聚会上不合适的游戏:”;
添加(新答案(“旋转瓶子”,41));
添加(新答案(“脱衣扑克”,32));
添加(新答案(“绕口令”,11));
添加(新答案(“真相或挑战”,11));
添加(新答案(“啤酒桶”,3));
打破
案例8:
ask=“说出一些会让你被赶出大多数酒吧的东西:”;
添加(新答案(“打架”,45));
添加(新答案(“饮酒过量”,29));
答案。添加(新答案(“不付款”,6));
答案。添加(新答案(“剥离”,5));
添加(新答案(“未成年”,3));
打破
案例9:
问=“说出你将在杰瑞·斯普林格秀上看到的东西:”;
添加(新答案(“战斗”,56));
添加(新答案(“裸体”,22));
答案。添加(新答案(“安全”,6));
添加(新答案(“Jerry Springer”,4));
添加(新答案(“扔椅子”,3));
打破
案例10:
ask=“说出一种可能用作看门狗的狗:”;
添加(新答案(“德国谢泼德”,36));
添加(新答案(“比特牛”,23));
添加(新答案(“杜宾犬”,20));
添加(新答案(“罗特韦尔”,8));
添加(新答案(“斗牛犬”,5));
打破
违约:
//够不着这个呵呵
}//结束开关
回问;
}
@凌驾
公共布尔等于(对象obj)
{
if(obj==null)
返回false;
如果(!Question.class.isAssignableFrom(obj.getClass()))
返回false;
最后一个问题q=(问题)obj;
if(q.getQuestion()==null | | this.getQuestion()==null)
雷图
Name something you would see on the Jerry Springer show:
Name something that would get you thrown out of most bars:
What accent might an American pretend to have in order to sound more attractive:
Name a breed of dog that might be used as a guard dog:
Name a place where a child might get seperated from its parents:
Name something you would see on the Jerry Springer show:
Name something that would get you thrown out of most bars:
Name a place where a child might get seperated from its parents:
Name something for which you need a warranty:
Name a game that would be inappropriate at a company party:
Name something for which you need a warranty:
Name a place where a child might get seperated from its parents:
Name an activity that could be rained out:
Name something for which you need a warranty:
Name something that would get you thrown out of most bars:
/**
 * Question proposed to players to guess one of the answers on the board
 *
 * Each question has 5 possible answers
 *
 * The top answer to each question is stored at Answers(0) and is worth the most
 * The last answer is stored at Answers(4) and is worth the least
 *
 *
 * @author Stefan Gligorevic
 */
import java.util.ArrayList;
import java.util.Random;
import java.lang.String;
import java.lang.Object;

public class Question {

    private ArrayList<Answer> answers;
    private String question;

    /**** MIGHT HAVE TO INITIALIZE ANSWERS TO AN ARRAY LIST WITH NO SIZE SO ADD CAN WORK ****/
    //constructors
    public Question(){
        answers = new ArrayList<>();
        question=getRandomQuestion();
    }

    //makes a random question with a set of answers
    public String getRandomQuestion() {
        String ask = "";

        Random rand = new Random();
        int n = 1 + rand.nextInt(10);

        switch (n)
        {
            case 1:
                ask="Name a place where a child might get seperated from its parents:";
                answers.add(new Answer("Mall", 38));
                answers.add(new Answer("Park", 23));
                answers.add(new Answer("Zoo", 16));
                answers.add(new Answer("Theme Park", 16));
                answers.add(new Answer("Airport", 5));
                break;
            case 2:
                ask="Name something for which you need a warranty:";
                answers.add(new Answer("Car", 54));
                answers.add(new Answer("TV", 23));
                answers.add(new Answer("Watch", 8));
                answers.add(new Answer("Computers", 4));
                answers.add(new Answer("Appliance", 3));
                break;
            case 3:
                ask="Name a fruit you can buy dried:";
                answers.add(new Answer("Grape", 22));
                answers.add(new Answer("Banana", 21));
                answers.add(new Answer("Apricot", 21));
                answers.add(new Answer("Prune", 17));
                answers.add(new Answer("Apple", 15));
                break;
            case 4:
                ask="Name an activity that could be rained out:";
                answers.add(new Answer("Sports Event", 45));
                answers.add(new Answer("Picnic", 34));
                answers.add(new Answer("Wedding", 10));
                answers.add(new Answer("Concert", 7));
                answers.add(new Answer("Barbecue", 3));
                break;
            case 5:
                ask="What accent might an American pretend to have in order to sound more attractive:";
                answers.add(new Answer("French", 61));
                answers.add(new Answer("British", 18));
                answers.add(new Answer("Italian", 8));
                answers.add(new Answer("Spanish", 8));
                answers.add(new Answer("Australian", 3));
                break;
            case 6:
                ask="Name a sport that might be played at a family reunion:";
                answers.add(new Answer("Football", 54));
                answers.add(new Answer("Baseball", 21));
                answers.add(new Answer("Horseshoe", 8));
                answers.add(new Answer("Frisbee", 7));
                answers.add(new Answer("Basketball", 6));
                break;
            case 7:
                ask="Name a game that would be inappropriate at a company party:";
                answers.add(new Answer("Spin the Bottle", 41));
                answers.add(new Answer("Strip Poker", 32));
                answers.add(new Answer("Twister", 11));
                answers.add(new Answer("Truth or Dare", 11));
                answers.add(new Answer("Beer Pong", 3));
                break;
            case 8:
                ask="Name something that would get you thrown out of most bars:";
                answers.add(new Answer("Getting in a fight", 45));
                answers.add(new Answer("Drinking too much", 29));
                answers.add(new Answer("Not Paying", 6));
                answers.add(new Answer("Stripping", 5));
                answers.add(new Answer("Being underage", 3));
                break;
            case 9:
                ask="Name something you would see on the Jerry Springer show:";
                answers.add(new Answer("Fighting", 56));
                answers.add(new Answer("Nudity", 22));
                answers.add(new Answer("Security", 6));
                answers.add(new Answer("Jerry Springer", 4));
                answers.add(new Answer("Chairs Thrown", 3));
                break;
            case 10:
                ask="Name a breed of dog that might be used as a guard dog:";
                answers.add(new Answer("German Shepard", 36));
                answers.add(new Answer("Pit Bull", 23));
                answers.add(new Answer("Doberman Pinscher", 20));
                answers.add(new Answer("Rottweiler", 8));
                answers.add(new Answer("Bulldog", 5));
                break;
            default:
                //won't reach this hehe
        } //end switch
        return ask;
    }

    @Override
    public boolean equals(Object obj)
    {
        if(obj == null)
            return false;

        if(!Question.class.isAssignableFrom(obj.getClass()))
            return false;

        final Question q = (Question) obj;

        if(q.getQuestion() == null || this.getQuestion() == null)
            return false;

        if(!this.getQuestion().equalsIgnoreCase(q.getQuestion()))
            return false;

        return true;
    }

    //getters and setters
    public String getQuestion() { return question; }
    public ArrayList<Answer> getAnswers() { return answers; }

    //returns answer at given index
    public Answer answerAt(int index) { return answers.get(index); }
    //returns String of answer at specified index
    public String getAnswerAt(int index) { return answers.get(index).getAnswer(); }
    //returns point value of answer at specified index
    public int getAnswerPoints(int index) { return answers.get(index).getValue(); }

    //returns the points earned for this question
    //points are the point values of all the answers that have been found
    public int pointsEarned() {
        int points=0;
        for (int i=0; i<answers.size(); i++)
        {
            if(answers.get(i).isFound())
                points += answers.get(i).getValue();
        }
        return points;
    }

    //Sets a particular answer's value
    public void setAnswerVal(int index, int val) { answers.get(index).setValue(val); }

    //Add a multiplier to each answer's value for the question
    public void addMultiplier(int multiplier)
    {
        for(int i=0; i<answers.size(); i++)
        {
            int val = answers.get(i).getValue();
            answers.get(i).setValue(multiplier * val);
        }
    }

    //returns string of each answer
    public String topAnswer() { return answers.get(0).getAnswer(); }
    public String Answer2() { return answers.get(1).getAnswer(); }
    public String Answer3() { return answers.get(2).getAnswer(); }
    public String Answer4() { return answers.get(3).getAnswer(); }
    public String lastAnswer() { return answers.get(4).getAnswer(); }

    //returns values of each answer
    public int topAnswerVal() { return answers.get(0).getValue(); }
    public int Answer2Val() { return answers.get(1).getValue(); }
    public int Answer3Val() { return answers.get(2).getValue(); }
    public int Answer4Val() { return answers.get(3).getValue(); }
    public int lastAnswerVal() { return answers.get(4).getValue(); }

}
    for(int i=0; i<15; i++)
    {
        Question q = new Question();
        //makes sure there are no duplicates
        if(!questions.contains(q))
            questions.add(q); //not working for some reason cry
    }
    for (Question question : questions) {
        System.out.println(question.getQuestion());
    }
public int hashCode(){
    return question.hashCode();
    }
@Override
public int compareTo(Question question) {
    return this.getQuestion().compareTo(question.getQuestion());
}
Set<Question> questions = new TreeSet<>();
    for(int i=0; i<15; i++)
    {
        questions.add(new Question());
    }
    for (Question question : questions) {
        System.out.println(question.getQuestion());
    }
Its better to use HashSet(if order of objects is not important)or LinkedHashSet(if 
order of objects is important) using an ArrayList will decrease the performance of the 
game on which you are currently working.
If you want to use LinkedHashSet you have to override hashCode and equals method.

eg program...

import java.util.LinkedHashSet;
public class Question {

    private ArrayList<Answer> answers;
    private String question;

    public Question(){
        answers = new ArrayList<>();
        question=getRandomQuestion();
    }
    @Override
    public int hashCode(){
        return question.hashCode();
    }
    @Override
    public boolean equals(Object o){
            if(o==null)return false;
            if(o.getClass()!=this.getClass())return false;
            Question q=(Question)o;
            return q.question.equals(this.question);
    }
    ...
    </>
    ...
    public static void main(String $[]){
        LinkedHashSet<Question>hash=new LinkedHashSet<>;
    }
}