为什么总是提示错误
麻烦老师帮我看一下为什么p1.addToPlayList(song1);这句没办法正常执行,调试提示说:musicList error,见最下面图
Song.java
package imooc.com.player;
//Song类
public class Song{
private String id, name, singer;//属性:歌曲id、歌曲名、演唱者
public Song(String id, String name, String singer) {
this.setId(id);
this.setName(name);
this.setSinger(singer);
}
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;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((singer == null) ? 0 : singer.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if(obj==this) {
return true;
}
if(obj.getClass()==Song.class) {
Song song=(Song)obj;
return song.getId().equals(id)&&song.getName().equals(name)&&song.getSinger().equals(singer);
}
return false;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "歌曲ID为:"+this.getId()+",歌曲名字为:"+this.getName()+",歌唱者为:"+this.getSinger()+" ";
}
}
PlayList.java
package imooc.com.player;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
//播放列表
public class PlayList {
private String playListName;// 播放列表名称
private List<Song> musicList;// 播放列表中的歌曲集合
// 构造方法
public PlayList(String playListName) {
this.setPlayListName(playListName);
this.musicList = new ArrayList<Song>();
}
// getter、setter
public String getPlayListName() {
return playListName;
}
public void setPlayListName(String playListName) {
this.playListName = playListName;
}
// 将歌曲添加到播放列表
public void addToPlayList(Song song) {
boolean flag = false;
for (Song song1:musicList) {
if (song1.equals(song)) {
flag = true;
System.out.println("啦啦啦啦啦");
break;
}
if(flag) {
System.out.println("该歌曲已存在");
}else {
musicList.add(song);
System.out.println("该歌曲添加成功!");
}
}
}
// 显示播放类别中所有歌曲
public void displayAllSong() {
System.out.println("列表中所有歌曲如下:");
for(Song song:this.musicList) {
System.out.println("123"+song);
}
}
// 通过ID查询歌曲
public Song searchSongById(String id) {
Song song=null;
for(Song song1:musicList) {
if(song1.getId().equals(id))
{
song=song1;
break;
}
}
return song;
}
//通过名称查询歌曲
public Song searchSongByName(String name) {
Song song=null;
for(Song song1:musicList) {
if(song1.getName().equals(name)) {
song=song1;
break;
}
}
return song;
}
//修改歌曲:将歌曲ID为id的歌修改为song
public void updateSong(String id, Song song) {
boolean flag=false;
for(Song song1:musicList) {
if(song1.getId().equals(id)) {
song1=song;
flag=true;
break;
}
if(flag){
System.out.println("修改成功!");
}else
System.out.println("要修改的歌曲id不存在!");
}
}
//从播放列表删除歌曲
public void deleteSong(String id) {
boolean flag=false;
for(Song song1:musicList) {
if(song1.getId().equals(id)) {
musicList.remove(song1);
flag=true;
break;
}
if(flag){
System.out.println("删除成功!");
}else
System.out.println("要删除的歌曲id不存在!");
}
}
//按歌曲名进行排序
public void sortBySongName() {
System.out.println("排序前:");
displayAllSong();
Collections.sort(musicList, new SongComparator());
System.out.println("排序后:");
displayAllSong();
}
}
TestClass.java
package imooc.com.player;
public class TestClass {
public void testSong() {
Song song1=new Song("s001","小太阳","小金人");
Song song2=new Song("s002","小月亮","小引人");
Song song3=new Song("s003","小地球","小铜人");
// Song song4=new Song("s003","小地球","小铜人");
// System.out.println(song4);
}
public void testPlayList() {
Song song1=new Song("s001","小太阳","小金人");
Song song2=new Song("s002","小月亮","小引人");
Song song3=new Song("s003","小地球","小铜人");
PlayList p1=new PlayList("播放列表1");
p1.addToPlayList(song1);
}
public static void main(String[] args) {
TestClass t=new TestClass();
t.testPlayList();
}
}
提示错误:
正在回答
同学你好,当musicList集合中没有元素时,进入不了for循环,所以,应该在循环结束后,对flag的值进行判断。
将if-else语句移至for循环之外。
其次,老师们会依据同学们提问的顺序一一进行调试、解答,所以参与学习的童鞋较多时,回复时间会有一定延迟。但是只要在问答区进行问题的有效描述,并贴好相关的代码和错误提示,老师一定会为大家解决问题哒!而且节假日也会有老师们值班帮助问题解答呦!加油加油!!
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星