【发布时间】:2019-04-23 11:59:40
【问题描述】:
如果任何歌曲包含对播放列表中上一首歌曲的引用,则该播放列表被视为重复播放列表。否则,播放列表将以最后一首指向 null 的歌曲结束。
我需要实现一个函数 isRepeatingPlaylist,如果播放列表重复则返回 true,否则返回 false。
例如,以下代码打印“true”,因为两首歌曲相互指向。
Song first = new Song("Hello");
Song second = new Song("Eye of the tiger");
first.setNextSong(second);
second.setNextSong(first);
System.out.println(first.isRepeatingPlaylist());
再一次,这不是家庭作业,我正在做编码挑战,因为当我阅读有关编程概念的理论时,我几乎可以理解,但是当面对编写程序时,我不知道从哪里开始,或者如何开始申请。
public class Song {
private String name;
private Song nextSong;
public Song(String name) {
this.name = name;
}
public void setNextSong(Song nextSong) {
this.nextSong = nextSong;
}
public boolean isRepeatingPlaylist() {
//throw new UnsupportedOperationException("Waiting to be implemented.");
List<String> list = new ArrayList<String>();
list.add(one);
list.add(two);
list.add(three);
list.add(four);
if list.contains()
return true;
else
return false;
}
public static void main(String[] args) {
Song first = new Song("Hello");
Song second = new Song("Eye of the tiger");
Song third = new Song("a test");
Song fourth = new Song("survivor");
first.setNextSong(second);
second.setNextSong(first);
System.out.println(first.isRepeatingPlaylist();
}
}
【问题讨论】:
-
您的意思是“相同”还是“相等”?您应该从编写可编译的代码开始。尝试类似: String t = getTestSong();返回 list.contains(t);