我有麻煩讓什麼使播放列表數組在用戶循環中工作。我可以在播放列表類中檢索歌曲列表數組,但User類內的循環不會接受播放列表類中的歌曲列表。任何幫助!?如何通過在用戶類中輸入播放列表數組中的位置來獲取歌曲標題?
請求:一種方法,返回位於播放列表中位置pos的歌曲的標題。輸出是空字符串,該位置不是有效的位置。
Playlist類:
public class Playlist {
//Instance Variables
private int numOfSongs;
private Songs[] songList;
// Constructors
public Playlist(int maxNumofSongs){
this.numOfSongs = 0;
this.songList = new Songs[maxNumofSongs];
}
//Getters
public Songs[] getSongList(){
return this.songList;
}
//Methods
public void addSong(String title, String filePath, String artist){
Songs p = new Songs(title, filePath, artist);
addSong(p);
}
public void addSong(Songs p){
songList[this.numOfSongs] = p;
this.numOfSongs++;
}
public Songs getSong(int pos){
if (pos <= this.songList.length)
return this.songList[pos];
else
return null;
}
public int getSongByTitle(String title){
int pos = -1;
for (int i = 0; i < this.numOfSongs; i++)
if (this.songList[i].getTitle() == title)
pos = i;
return pos;
}
public String toString(){
String playlistDesc = "";
playlistDesc += "Number of Songs added in Playlist: "+ numOfSongs;
return playlistDesc;
}
}
用戶類別:
public class User {
//Instance Variables
private String name;
private String email;
private Playlist favoriteSongs;
//Constructors
public User(String name, String email, Playlist favoriteSongs){
this.name = name;
this.email = email;
Songs[] songs = this.favoriteSongs.getSongList();
}
public User(String name, String email){
this.name = name;
this.email = email;
}
//Setters
public void setPlayList(Playlist list){
this.favoriteSongs = list;
}
//Get song title by inputting the position in the playlist array
public String getSongTitle(int pos){
if (pos == this.favoriteSongs.getSongList())
return Playlist.length[pos];
}
//Add new song to the playlist
public int addSong(String title, String filePath, String artist){
for(int i=0; i<this.Playlist.length; i++) {
if ((this.songList[i].getTitle().getArtist() == addSong(i))) // or what ever you want to compare
return 0;
}
// if you do not found any thing
return -1;
}
}
// Counts how many songs with the same artist
public int artistSongCount(String artist){
int count = 0;
for (int i=0; i < Playlist.numOfSongs.length; i++)
if (this.favoriteSongs[i].getArtist() == artist)
count++;
return count;
}
}
//Print out details of user
public String toString(){
String userOutput = "";
userOutput += "Name: "+ name;
userOutput += "Email: "+ email;
return userOutput;
}
}
爲了讓你開始,你引用了你的用戶類中的類「播放列表」,而不是變量(favoriteSongs)。還要看看播放列表中的變量numOfSongs,它是一個私有變量,並且您試圖在User中調用它。 – user1875195