debug到PlayList的 for(Song song1:musicList) { 直接跳出
//Song public class Song { private String songId; private String songName; private String singer; public Song(String songId, String songName, String singer) { this.songId = songId; this.songName = songName; this.singer = singer; } public String getSongId() { return songId; } public void setSongId(String songId) { this.songId = songId; } public String getSongName() { return songName; } public void setSongName(String songName) { this.songName = songName; } public String getSinger() { return singer; } public void setSinger(String singer) { this.singer = singer; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((singer == null) ? 0 : singer.hashCode()); result = prime * result + ((songId == null) ? 0 : songId.hashCode()); result = prime * result + ((songName == null) ? 0 : songName.hashCode()); return result; } @Override public boolean equals(Object obj) { //判断对象是否相等 if(this==obj) return true; //this!=obj if (obj.getClass() == Song.class) { Song song = (Song) obj; return (song.getSongId()).equals(songId) && (song.getSongName()).equals(songName) && (song.getSinger()).equals(singer); } return false; } @Override public String toString() { return "歌曲信息:\n序号:" + songId + "\n歌曲名称:" + songName + "\n歌手:" + singer; } }//PlayList import java.util.ArrayList; import java.util.List; public class PlayList { private String playListName; //播放列表名称 private List<Song> musicList; //播放列表的歌曲集合 /** * 构造方法 * @param playListName 播放列表的名称 */ public PlayList(String playListName) { this.playListName=playListName; musicList=new ArrayList<Song>(); } /** * 将歌曲添加到播放列表 * @param song 要添加的歌曲 */ public void addToPlayList(Song song) { //排除重复添加的情况 boolean flag=false; //判断播放列表中的歌曲是否存在 for(Song song1:musicList) { //到这一句就无法进入内部循环判断 if(song1.equals(song)) { flag=true; break; } if(flag) { System.out.println("该歌曲已经存在于播放列表中,添加失败"); }else { musicList.add(song); } } } /** * 显示播放列表中的所有歌曲 */ public void displayAllSong() { System.out.println("播放列表中的所有歌曲为:"); for(Song song:musicList) { System.out.println(song); } } } //TestDemo public class TestDemo { //对播放列表类PlayList进行测试 public void testPlayList() { //定义几个对象添加到播放列表中 Song song1 =new Song("s001","七里香","周杰伦"); Song song2 =new Song("s002","挪威的森林","伍佰"); Song song3 =new Song("s003","心中的日月","王力宏"); //Song song4 =new Song("s003","心中的日月","王力宏"); //创建一个PlayList对象 PlayList mainPlayList=new PlayList("主播放列表"); mainPlayList.addToPlayList(song1); mainPlayList.addToPlayList(song2); mainPlayList.addToPlayList(song3); //显示播放列表的内容 mainPlayList.displayAllSong(); } public static void main(String[] args) { TestDemo td=new TestDemo(); //td.testSong(); td.testPlayList(); } } 运行结果只有: 播放列表中的所有歌曲为:
2
收起
正在回答 回答被采纳积分+1
3回答
KazamiYuuka
2019-05-05 11:04:01
Iterator it=musicList.iterator(); Song song1=null; for(int i=0;i<musicList.size();i++) { song1=(Song)it.next(); if(song1.equals(song)) { flag=true; break; } if(flag) { System.out.println("该歌曲已经存在于播放列表中,添加失败"); }else { musicList.add(song); } }
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星