关于歌曲id的唯一性
package Player; import java.util.Objects; public class Song { //属性:id,name,singer,format private String id, name, singer, format; //无参构造方法 public Song() { } //3参构造方法 public Song(String id, String name, String singer, String format) { this.id = id; this.name = name; this.singer = singer; this.format = format; } //get set方法 public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSinger() { return singer; } public void setSinger(String singer) { this.singer = singer; } public String getFormat() { return format; } public void setFormat(String format) { this.format = format; } //hashcode()equals()方法 @Override public boolean equals(Object o) { //判断对象是否相等 if (this == o) { return true; } //判断是否是属于子类 if (o.getClass() == Song.class) { Song song = (Song) o; return (song.getId().equalsIgnoreCase(id)) && (song.getName().equalsIgnoreCase(name)) && (song.getSinger().equalsIgnoreCase(singer)) && (song.getFormat().equalsIgnoreCase(format)); } return false; } @Override public int hashCode() { int result = id.hashCode(); result = 31 * result + name.hashCode(); result = 31 * result + singer.hashCode(); result = 31 * result + format.hashCode(); return result; } //toString方法 @Override public String toString() { return "歌曲信息:" + "\n歌曲id:" + this.getId() + "\n歌曲名:" + this.getName() + "\n演唱者:" + this.getSinger() + "\n格式:" + this.getFormat() + "\n"; } } package Player; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class PlayList { //属性:播放列表名称,歌曲集合 private String playListName; private List<Song> musicList; //无参构造方法 public PlayList() { } //有参构造 public PlayList(String playListName) { this.playListName = playListName; musicList = new ArrayList<>(); } //get set方法 public String getPlayListName() { return playListName; } public void setPlayListName(String playListName) { this.playListName = playListName; } public List<Song> getMusicList() { return musicList; } public void setMusicList(List<Song> musicList) { this.musicList = musicList; } //添加歌曲 public void addToPalyList(Song song) { boolean flag = false; for (Song s : musicList) { if (s.equals(song)) { flag = true; break; } } if (flag) { System.out.println(song.getName() + "已存在于此列表中,无法添加!"); } else { musicList.add(song); } } //修改歌曲信息(id) public void updateSong(String id, Song song) { //查询歌曲id是否存在 Song song1 = null; for (Song s : musicList) { if (s.getId().equalsIgnoreCase(id)) { song1 = s; break; } } if (song1 == null) { System.out.println("没有找到id为" + id + "的对应歌曲信息!"); } else { //先移除,后添加 musicList.remove(song1); musicList.add(song); System.out.println("修改成功!"); } } //删除歌曲(id) public void deleteSongById(String id) { //先判断歌曲id是否在列表中 Song s = null; for (Song song : musicList) { if (song.getId().equalsIgnoreCase(id)) { s = song; break; } } if (s != null) { musicList.remove(s); System.out.println("该歌曲已从列表中删除!"); } else { System.out.println("该歌曲不存在!"); } } //删除歌曲(根据歌曲名字,歌名不唯一,删除集合中多个元素) public void deleteSongByName(String name) { //使用迭代器遍历修改 int n = musicList.size(); Iterator<Song> iterator = musicList.iterator(); while (iterator.hasNext()) { Song song = iterator.next(); if (name.equalsIgnoreCase(song.getName())) { //注意不能用List的remove方法 iterator.remove(); } } int m = musicList.size(); if (m == n) { System.out.println("该歌名的歌曲不存在!"); } else { System.out.println("删除成功!"); } } //删除歌曲(根据歌曲演唱者,演唱者不唯一,删除集合中多个元素) public void deleteSongBySinger(String singer) { //使用迭代器遍历修改 int n = musicList.size(); Iterator<Song> iterator = musicList.iterator(); while (iterator.hasNext()) { Song song = iterator.next(); if (singer.equalsIgnoreCase(song.getSinger())) { //注意不能用List的remove方法 iterator.remove(); } } int m = musicList.size(); if (m == n) { System.out.println("该歌手的歌曲不存在!"); } else { System.out.println("删除成功!"); } } //删除歌曲(根据歌曲格式,格式不唯一,删除集合中多个元素) public void deleteSongByFormat(String format) { //使用迭代器遍历修改 int n = musicList.size(); Iterator<Song> iterator = musicList.iterator(); while (iterator.hasNext()) { Song song = iterator.next(); if (format.equalsIgnoreCase(song.getFormat())) { //注意不能用List的remove方法 iterator.remove(); } } int m = musicList.size(); if (m == n) { System.out.println("该格式的歌曲不存在!"); } else { System.out.println("删除成功!"); } } //显示播放列表中所有歌曲 public void displayAllSong() { System.out.println("主播放列表中所有的歌曲:"); for (Song song : musicList) { System.out.println(song); } } //根据id找歌(唯一性) public void searchSongById(String id) { Song song = null; for (Song s : musicList) { if (s.getId().equalsIgnoreCase(id)) { song = s; break; } } if (song == null) { System.out.println("该列表中没有此歌曲!"); } else { System.out.println("根据歌曲id查询到的歌曲信息为:"); System.out.println(song); } } //根据歌名找歌(由于格式不同,可能出现同歌名情况) public void searchSongByName(String name) { List<Song> list = new ArrayList<>(); for (Song s : musicList) { if (s.getName().equalsIgnoreCase(name)) { list.add(s); } } if (list.size() == 0) { System.out.println("该列表中没有此歌曲!"); } else { System.out.println(name + "的歌曲信息为:"); for (Song s : list) { System.out.println(s); } } } //根据格式找歌(不唯一性,所以返回值可能是多个,可以用集合表示) public void searchByFormat(String format) { List<Song> list = new ArrayList<Song>(); int count = 0; for (Song song : musicList) { if (song.getFormat().equalsIgnoreCase(format)) { list.add(song); count++; } } if (list.size() != 0) { System.out.println(format + "格式的歌曲有:" + count + "首"); int num = 1; for (Song s : list) { System.out.println("第" + num + "首" + "歌名为:" + s.getName()); } } else { System.out.println("该格式的歌曲不存在!"); } } //根据歌手找歌 public void searchBySinger(String singer) { List<Song> list = new ArrayList<Song>(); int count = 0; for (Song song : musicList) { if (song.getSinger().equalsIgnoreCase(singer)) { list.add(song); count++; } } if (list.size() != 0) { System.out.println(singer + "的歌曲有:" + count + "首"); int num = 1; for (Song s : list) { System.out.println("第" + num + "首" + "歌名为:" + s.getName()); } } else { System.out.println(singer + "的歌曲不存在!"); } } } package Player; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class PlayList { //属性:播放列表名称,歌曲集合 private String playListName; private List<Song> musicList; //无参构造方法 public PlayList() { } //有参构造 public PlayList(String playListName) { this.playListName = playListName; musicList = new ArrayList<>(); } //get set方法 public String getPlayListName() { return playListName; } public void setPlayListName(String playListName) { this.playListName = playListName; } public List<Song> getMusicList() { return musicList; } public void setMusicList(List<Song> musicList) { this.musicList = musicList; } //添加歌曲 public void addToPalyList(Song song) { boolean flag = false; for (Song s : musicList) { if (s.equals(song)) { flag = true; break; } } if (flag) { System.out.println(song.getName() + "已存在于此列表中,无法添加!"); } else { musicList.add(song); } } //修改歌曲信息(id) public void updateSong(String id, Song song) { //查询歌曲id是否存在 Song song1 = null; for (Song s : musicList) { if (s.getId().equalsIgnoreCase(id)) { song1 = s; break; } } if (song1 == null) { System.out.println("没有找到id为" + id + "的对应歌曲信息!"); } else { //先移除,后添加 musicList.remove(song1); musicList.add(song); System.out.println("修改成功!"); } } //删除歌曲(id) public void deleteSongById(String id) { //先判断歌曲id是否在列表中 Song s = null; for (Song song : musicList) { if (song.getId().equalsIgnoreCase(id)) { s = song; break; } } if (s != null) { musicList.remove(s); System.out.println("该歌曲已从列表中删除!"); } else { System.out.println("该歌曲不存在!"); } } //删除歌曲(根据歌曲名字,歌名不唯一,删除集合中多个元素) public void deleteSongByName(String name) { //使用迭代器遍历修改 int n = musicList.size(); Iterator<Song> iterator = musicList.iterator(); while (iterator.hasNext()) { Song song = iterator.next(); if (name.equalsIgnoreCase(song.getName())) { //注意不能用List的remove方法 iterator.remove(); } } int m = musicList.size(); if (m == n) { System.out.println("该歌名的歌曲不存在!"); } else { System.out.println("删除成功!"); } } //删除歌曲(根据歌曲演唱者,演唱者不唯一,删除集合中多个元素) public void deleteSongBySinger(String singer) { //使用迭代器遍历修改 int n = musicList.size(); Iterator<Song> iterator = musicList.iterator(); while (iterator.hasNext()) { Song song = iterator.next(); if (singer.equalsIgnoreCase(song.getSinger())) { //注意不能用List的remove方法 iterator.remove(); } } int m = musicList.size(); if (m == n) { System.out.println("该歌手的歌曲不存在!"); } else { System.out.println("删除成功!"); } } //删除歌曲(根据歌曲格式,格式不唯一,删除集合中多个元素) public void deleteSongByFormat(String format) { //使用迭代器遍历修改 int n = musicList.size(); Iterator<Song> iterator = musicList.iterator(); while (iterator.hasNext()) { Song song = iterator.next(); if (format.equalsIgnoreCase(song.getFormat())) { //注意不能用List的remove方法 iterator.remove(); } } int m = musicList.size(); if (m == n) { System.out.println("该格式的歌曲不存在!"); } else { System.out.println("删除成功!"); } } //显示播放列表中所有歌曲 public void displayAllSong() { System.out.println("主播放列表中所有的歌曲:"); for (Song song : musicList) { System.out.println(song); } } //根据id找歌(唯一性) public void searchSongById(String id) { Song song = null; for (Song s : musicList) { if (s.getId().equalsIgnoreCase(id)) { song = s; break; } } if (song == null) { System.out.println("该列表中没有此歌曲!"); } else { System.out.println("根据歌曲id查询到的歌曲信息为:"); System.out.println(song); } } //根据歌名找歌(由于格式不同,可能出现同歌名情况) public void searchSongByName(String name) { List<Song> list = new ArrayList<>(); for (Song s : musicList) { if (s.getName().equalsIgnoreCase(name)) { list.add(s); } } if (list.size() == 0) { System.out.println("该列表中没有此歌曲!"); } else { System.out.println(name + "的歌曲信息为:"); for (Song s : list) { System.out.println(s); } } } //根据格式找歌(不唯一性,所以返回值可能是多个,可以用集合表示) public void searchByFormat(String format) { List<Song> list = new ArrayList<Song>(); int count = 0; for (Song song : musicList) { if (song.getFormat().equalsIgnoreCase(format)) { list.add(song); count++; } } if (list.size() != 0) { System.out.println(format + "格式的歌曲有:" + count + "首"); int num = 1; for (Song s : list) { System.out.println("第" + num + "首" + "歌名为:" + s.getName()); } } else { System.out.println("该格式的歌曲不存在!"); } } //根据歌手找歌 public void searchBySinger(String singer) { List<Song> list = new ArrayList<Song>(); int count = 0; for (Song song : musicList) { if (song.getSinger().equalsIgnoreCase(singer)) { list.add(song); count++; } } if (list.size() != 0) { System.out.println(singer + "的歌曲有:" + count + "首"); int num = 1; for (Song s : list) { System.out.println("第" + num + "首" + "歌名为:" + s.getName()); } } else { System.out.println(singer + "的歌曲不存在!"); } } } package Player; import org.w3c.dom.ls.LSOutput; import java.util.List; public class Test { //对歌曲类的测试 public void testSong() { Song s1 = new Song("s0001", "青花瓷", "周杰伦", "MP3"); Song s2 = new Song("s0002", "红豆", "王菲", "WMA"); Song s3 = new Song("s0003", "山丘", "李宗盛", "APE"); Song s4 = new Song("s0004", "消愁", "毛不易", "FLAC"); Song s5 = new Song("s0005", "听海", "张惠妹", "APE"); System.out.println(s5); //测试对象是否相等 // System.out.println(s1.equals(s2)); } //对播放列表类中的方法测试 public void TestPlayList() { //定义song对象 Song s1 = new Song("s0001", "青花瓷", "周杰伦", "MP3"); Song s2 = new Song("s0002", "红豆", "王菲", "WMA"); Song s3 = new Song("s0003", "山丘", "李宗盛", "APE"); Song s4 = new Song("s0004", "消愁", "毛不易", "FLAC"); Song s5 = new Song("s0005", "听海", "张惠妹", "APE"); Song s6 = new Song("s0006", "青花瓷", "周杰伦", "APE"); Song s7 = new Song("s0002", "暧昧", "王菲", "WMA"); //创建PlayLis对象 PlayList pl = new PlayList("主播放列表"); pl.addToPalyList(s1); pl.addToPalyList(s2); pl.addToPalyList(s3); pl.addToPalyList(s4); pl.addToPalyList(s5); pl.addToPalyList(s6); pl.addToPalyList(s7); //显示播放列表 pl.displayAllSong(); //id找歌 pl.searchSongById("S0002"); //歌名找歌 // pl.searchSongByName("青花瓷"); //歌手找歌 // pl.searchBySinger("毛不易"); //格式找歌 // pl.searchByFormat("Ape"); //删除歌曲(id) // pl.deleteSongById("02"); //删除歌曲(名字) // pl.deleteSongByName("青花瓷"); //删除歌曲(歌手) // pl.deleteSongBySinger("周杰伦"); //删除歌曲(格式) // pl.deleteSongByFormat("ape"); //修改歌曲(id) // pl.updateSong("02",s7); // pl.displayAllSong(); } public static void main(String[] args) { Test t = new Test(); // t.testSong(); t.TestPlayList(); } }
歌曲id好像不是唯一性的,我添加两个Song对象,id一样,还是只能查询到一个?
5
收起
正在回答
1回答
同学,你好!之所以只有一个id为s0002的歌曲,是因为在查询歌曲时,只要查询到符合条件的歌曲就跳出循环了,后面即使再有id相同的歌曲也查询不到了。
如果同学想要id也唯一,那么在添加歌曲时,可以判断一下id是否存在,如果不存在再添加歌曲。
另外,包名建议所有字母均小写。再有,同学贴出的代码中,PlayList类贴了两遍,没有PlayListCollection类
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星