2

这是我用于我的课程的代码。我已经测试了很多次,如果我不序列化和反序列化,这些功能都可以正常工作。

public class Library {

    private String libTitle;
    private Vector<Album> albums;

    public String getLibTitle() {
        return libTitle;
    }

    public void setLibTitle(String libTitle) {
        this.libTitle = libTitle;
}

public Vector<Album> getAlbums() {
    return albums;
}

public void setAlbums(Vector<Album> albums) {
    this.albums = albums;
}

public Library(){
}

public Library(String libTitle) {
    this.libTitle = libTitle;
    this.albums = new Vector<Album>();
    albums.trimToSize();

}


public void addAlbum(String album){
    boolean added = false;
    for (Album alb: this.getAlbums()){
        if (alb.getAlbum()==album){
            added=true;
        }
        if (added){
            break;
        }
    }
    if (!added){
        this.getAlbums().add(new Album(album));
        this.getAlbums().trimToSize();
    }
}

public void removeAlbum(String album){
    for (Album alb : this.getAlbums()){
        if (alb.getAlbum()==album){
            this.getAlbums().remove(alb);
            this.getAlbums().trimToSize();
            break;
        }
    }
}

public void addSong(String title, String author, String album){
    boolean added = false;
    for (Album alb : this.getAlbums()){
        if (alb.getAlbum()==album){
            alb.addSong(title,author);
            added=true;
        }
        if (added){
            break;
        }
    }
    if (!added){
        this.addAlbum(album);
        this.addSong(title, author, album);
    }
}

public void removeSong(String title, String author, String album){
    boolean removed = false;
    for (Album alb : this.getAlbums()){
        if (alb.getAlbum()==album){
            alb.removeSong(title);
            if(alb.getSongs().isEmpty()){
                this.getAlbums().remove(alb);
                this.getAlbums().trimToSize();
            };
            removed=true;
        }
        if (removed){
            break;
        }
    }       
}

public void save()
{
    try {
        FileOutputStream xmlos = new FileOutputStream(this.libTitle +".xml");
            XMLEncoder encoder = new XMLEncoder(xmlos);
        encoder.writeObject(this);
        encoder.close();
        xmlos.close();
        System.out.println("Done exporting a user as xml to "+this.libTitle+".xml");
    }catch(Exception e) {
        e.printStackTrace();
    }
}

public Library restore(String lib)
{
    Library newLib = null;
    try {
        System.out.println("Importing a user as xml from "+lib+".xml");
        FileInputStream inFileStream = new FileInputStream(lib +".xml");
        XMLDecoder decoder = new XMLDecoder(inFileStream);
        newLib = (Library)decoder.readObject();
        decoder.close();
        inFileStream.close();
        System.out.println("Libloaded "+ newLib.getLibTitle());
        return newLib;

    }catch(Exception e) {
        e.printStackTrace();
    }
    return newLib;

}

}

接下来是相册类,在库类中使用

public class Album {

private String album;
private Vector<Song> songs;

public Vector<Song> getSongs() {
    return songs;
}

public void setSongs(Vector<Song> songs) {
    this.songs = songs;
}

public Album(){
}

public Album(String album) {
    this.album = album;
    this.songs = new Vector<Song>();
    songs.trimToSize();

}

public String getAlbum() {
    return album;
}

public void setAlbum(String album) {
    this.album = album;
}

public void addSong(String song, String author){
    boolean added = false;
    for (Song son : this.getSongs()){
        if (son.getTitle()==song){
            added=true;
        }
        if (added){
            break;
        }
    }
    if (!added){
        this.getSongs().add(new Song(song, author));
        this.getSongs().trimToSize();
    }
}

public void removeSong(String song){
    for (Song son : this.getSongs()){
        if (son.getTitle()==song){
            this.getSongs().remove(son);
            this.getSongs().trimToSize();
            break;
        }
    }

}

}

之后是歌曲类。

public class Song {

private String title,author;

public Song(){
}

public Song(String title, String author) {
    this.title = title;
    this.author = author;
}

public String getTitle() {
    return title;
}

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

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}
}

所以基本上它是一个库,我正在序列化为 XML,然后反序列化我正在制作的应用程序。我的问题是当我运行以下代码时......

public class Run {


public static void main(String args[]){
    Library lib = new Library("MYLibrary");
    lib.addSong("Crawling", "Lincoln Park", "Hybrid Theory");
    lib.addSong("In The End", "Lincoln Park", "Hybrid Theory");
    lib.addSong("Fire", "Pyros", "Burning Up");
    lib.addSong("Ocean", "Drowners", "Burning Up");
    lib.save();
    Library lib2 = new Library();
    lib2 =lib2.restore("MYLibrary");
    lib2.setLibTitle("test");
    lib2.removeSong("In The End", "Lincoln Park", "Hybrid Theory");
    lib2.addSong("Crawling in the dark", "me", "Hybrid Theory");
    lib2.removeSong("Crawling in the dark", "me", "Hybrid Theory");
    lib2.removeSong("Ocean", "Drowners", "Burning Up");
    lib2.removeAlbum("Hybrid Theory");
    lib2.save();

}

}

保存的 XML 文件不会只有一首歌曲,因为它应该生成的两个 XML 文件是相同的。我的老师不明白为什么它不起作用,我也不明白。为什么图书馆不改变?

4

2 回答 2

3

您正在使用==not比较专辑和歌曲标题.equals()。如果对象被序列化和反序列化,这将不起作用,因为对象引用将发生变化。

更一般地说,在大多数情况下,您应该将字符串与.equals()非字符串进行比较。==例如:

new String("test") == "test"

将评估为false。有关更多解释,请参阅此问题

如果您的 Album 和 Song 类覆盖.equals()andhashcode()方法会更好,这样您就可以直接比较它们,而不必提取它们的标题并进行比较。

于 2012-10-03T21:58:47.177 回答
0

所有具有字符串比较的方法都==用于代替equals(). ==运算符比较对象实例。由于两个对象实例不相同,因此false在所有情况下都返回。更新方法以使用正确的比较方法,例如您的 removeAlbum 方法应更新如下:

public void removeAlbum(String album) {
    for (Album alb : this.getAlbums()) {
        if (alb.getAlbum().equals(album)) {
            this.getAlbums().remove(alb);
            this.getAlbums().trimToSize();
            break;
        }
    }
}

我也建议equals在你的类定义中实现(覆盖)方法。当您在类中添加更多属性时,这会有所帮助。

于 2012-10-03T22:43:43.133 回答