检查一个对象中的两个参数是否已经存在于arrayList中,如果是,则不添加到arrayList中?

时间:2017-11-22 16:29:50

标签: java object arraylist

当尝试识别arrayList中是否存在重复项时,它似乎没有检查,我试图覆盖equals但是语句将始终传递为false并将该对象添加到列表中。在动作监听器中我的陈述有什么问题?

问题出在动作侦听器语句中。

window.localStorage.getItem('phpInfo')

音乐课程:

public class addTrackLayout extends JFrame{

private JTextField artist; 
private JTextField trackTitle;
private JTextField genre;
private JTextField duration;
private JTextField year;

private JLabel artistLabel;
private JLabel titleLabel;
private JLabel genreLabel;
private JLabel durationLabel;
private JLabel yearLabel;
private JLabel addLabel;

private String addArtist;
private String addTitle;
private String addGenre;
private double addDuration;
private int addYear;

private static final long serialVersionUID = 1L;

public addTrackLayout(String title){

    super (title);

    JPanel contentPane = new JPanel();
    contentPane.setLayout(new GridBagLayout());

    addLabel = new JLabel("Add Record");
    artistLabel = new JLabel("Artist Name:");
    titleLabel = new JLabel("Track Title:");
    genreLabel = new JLabel("Music Genre:");
    durationLabel = new JLabel("Duration:");
    yearLabel = new JLabel("Release Year:");

    artist = new JTextField(15);
    trackTitle = new JTextField(15);
    genre = new JTextField(15);
    duration = new JTextField(15);
    year = new JTextField(15);
    ArrayList<Music> music = TimelineLayout.music;

    JButton addButton = new JButton("Add");
    addButton.setActionCommand("add");


        //Actions for buttons.
        addButton.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e){

                    addArtist = artist.getText();
                    addTitle = trackTitle.getText();
                    addGenre = genre.getText();
                    addDuration = Double.parseDouble(duration.getText());
                    addYear = Integer.parseInt(year.getText());

                    Music m = new Music(addArtist, addTitle, addGenre, addDuration, addYear);


                    if(music.contains(m)){

                        JOptionPane.showMessageDialog(null, "already exists");
                    }
                    else{

                        music.add(m);       
                        JOptionPane.showMessageDialog(null,"added to db");
                    }

                }   

            });

覆盖和哈希。

public class Music {

private String artist;
private String title;
private String genre;
private double duration;
private int year;


public Music(String initArtist, String initTitle, String initGenre, double initDuration, int initYear)
{   
    artist =  initArtist;
    title= initTitle;
    genre = initGenre;
    duration = initDuration;
    year = initYear;
}

public String getArtist() {
    return artist;
}

public void setArtist(String artist) {
    this.artist = artist;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public double getDuration() {
    return duration;
}

public void setDuration(double duration) {
    this.duration = duration;
}

public int getYear() {
    return year;
}

public void setYear(int year) {
    this.year = year;
}

public String getGenre() {
    return genre;
}

public void setGenre(String genre) {
    this.genre = genre;
}

2 个答案:

答案 0 :(得分:0)

问题在于您需要了解Java中的对象相等性,以及如何在ArrayList中实现它。

方法list.contains(obj)

  

至少有一个元素e(o == null?e == null:o.equals(e))。

(来自https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#contains(java.lang.Object)

这意味着数组列表正在运行Music.equals(...)

我只能假设您没有覆盖.equals对象中的Music,以确保可以比较对象。

快速复习:

obj == obj

检查对象是否指向同一个东西。所以......

Music m;
Music m2;
m == m2; //would be false, they're seperate.
m.equals(m2) //you should implement this so it is true.

你说你想看看数组中是否已经存在一个元素。

这是通过在equals(Object o)类中实施Music来完成的。

@override
public boolean equals(Object o){
     boolean equals = true;
     if(o instanceof Music){
        //here, we need to compare all the fields in music
        //repeat for title, genre, duration, year, etc.

        if(!(Music) o.artist.equals( this.artist){
             equals = false;
        }
    }
    return equals;
}

进一步阅读:

  1. Compare two objects with .equals() and == operator - 请特别注意标记为已接受的答案,如果您要比较自定义类的实例,则需要覆盖equals()方法,因此, hashCode()方法。

  2. https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html - ArrayList的所有方法 - 要真正学习数据结构,你应该记住它。

答案 1 :(得分:0)

您必须覆盖Music类中的hashcode和equals方法,并实现类似的界面。当您将新音乐添加到音乐列表时,请检查您的音乐列表是否包含音乐列表,如果没有添加它。