创建每个元素都有几个参数的数组
Creating the array in which every element has couple of parameters
我需要创建 array/arraylist(不知道在这种情况下什么更好),其中数组的每个元素都有 3 个参数。
它必须是一组歌曲(元素)并且每首歌曲必须具有这 3 个参数(标题、名称、持续时间)。稍后我需要计算每首歌曲的所有持续时间。
在 addSong
方法中,我想创建将成为 ArrayList 的 1 个元素的数组,但它无法正常工作。感谢帮助。
public class Jukebox extends Song {
public ArrayList<String> songs = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public void addSong() throws IOException {
String[] array = new String[3];
System.out.println("Composer:");
array[0] = reader.readLine();
System.out.println("Title");
array[1] = reader.readLine();
System.out.println("Duration");
array[2] = reader.readLine();
ArrayList<String>songs = new ArrayList<String>(Arrays.asList(array));
this.songs = songs;
//Song newSong = new Song();
//System.out.println("Composer: ");
//newSong.composer = reader.readLine();
//System.out.println(" Title: ");
//newSong.title=reader.readLine();
//System.out.println(" Duration: ");
//newSong.duration = Double.parseDouble(reader.readLine());
//songs.add(newSong);
}
public void playAll() {
for (int i = 0; i < songs.size(); i++) {
int j = songs.size() - i - 1;
System.out.print(songs.get(j) + " ");
}
}
}
我已经创建了 class 歌曲,我只是没有在这里添加它。
class Song {
String composer;
String title;
double duration;
public String getComposer() {
return composer;
}
public String getTitle() {
return title;
}
public double getDuration() {
return duration;
}
@Override
public String toString() {
return title + composer + duration ;
}
}
你能制作一首包含这三个字段的歌曲 class 并将其存储在列表中吗?
您想要创建一个包含 3 个字段的歌曲 class 并将歌曲实例添加到 ArrayList。
创作歌曲class
public class Song{
private String composer;
private String title;
private String duration; //you can even take it as Int
//write the codes for getters and setters over here
}
然后定义一个List:
List<Song> songs=new ArrayList<Song>();
创建 Song
class.
public class Song{
String composer, title;
int duration;
public Song(String composer, String title, int duration){
this.composer = composer;
this.title = title;
this.duration = duration;
}
// getter & setters
}
为每首歌曲创建一个 Song
对象
System.out.println("Composer:");
String composer = reader.readLine();
System.out.println("Title");
String title = reader.readLine();
System.out.println("Duration");
int duration = Integer.parseInt(reader.readLine());
Song song = new Song(composer,title,duration);
将所有内容添加到 List<Song>
//create a `ArrayList` before
List<Song> songs = new ArrayList<>();
// add songs
songs.add(song)
获取所有时长
int totalDuration = 0;
for(Song s : songs){
totalDuration += s.getDuration();
}
我需要创建 array/arraylist(不知道在这种情况下什么更好),其中数组的每个元素都有 3 个参数。 它必须是一组歌曲(元素)并且每首歌曲必须具有这 3 个参数(标题、名称、持续时间)。稍后我需要计算每首歌曲的所有持续时间。
在 addSong
方法中,我想创建将成为 ArrayList 的 1 个元素的数组,但它无法正常工作。感谢帮助。
public class Jukebox extends Song {
public ArrayList<String> songs = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public void addSong() throws IOException {
String[] array = new String[3];
System.out.println("Composer:");
array[0] = reader.readLine();
System.out.println("Title");
array[1] = reader.readLine();
System.out.println("Duration");
array[2] = reader.readLine();
ArrayList<String>songs = new ArrayList<String>(Arrays.asList(array));
this.songs = songs;
//Song newSong = new Song();
//System.out.println("Composer: ");
//newSong.composer = reader.readLine();
//System.out.println(" Title: ");
//newSong.title=reader.readLine();
//System.out.println(" Duration: ");
//newSong.duration = Double.parseDouble(reader.readLine());
//songs.add(newSong);
}
public void playAll() {
for (int i = 0; i < songs.size(); i++) {
int j = songs.size() - i - 1;
System.out.print(songs.get(j) + " ");
}
}
}
我已经创建了 class 歌曲,我只是没有在这里添加它。
class Song {
String composer;
String title;
double duration;
public String getComposer() {
return composer;
}
public String getTitle() {
return title;
}
public double getDuration() {
return duration;
}
@Override
public String toString() {
return title + composer + duration ;
}
}
你能制作一首包含这三个字段的歌曲 class 并将其存储在列表中吗?
您想要创建一个包含 3 个字段的歌曲 class 并将歌曲实例添加到 ArrayList。
创作歌曲class
public class Song{
private String composer;
private String title;
private String duration; //you can even take it as Int
//write the codes for getters and setters over here
}
然后定义一个List:
List<Song> songs=new ArrayList<Song>();
创建 Song
class.
public class Song{
String composer, title;
int duration;
public Song(String composer, String title, int duration){
this.composer = composer;
this.title = title;
this.duration = duration;
}
// getter & setters
}
为每首歌曲创建一个 Song
对象
System.out.println("Composer:");
String composer = reader.readLine();
System.out.println("Title");
String title = reader.readLine();
System.out.println("Duration");
int duration = Integer.parseInt(reader.readLine());
Song song = new Song(composer,title,duration);
将所有内容添加到 List<Song>
//create a `ArrayList` before
List<Song> songs = new ArrayList<>();
// add songs
songs.add(song)
获取所有时长
int totalDuration = 0;
for(Song s : songs){
totalDuration += s.getDuration();
}