【发布时间】:2013-12-28 08:05:06
【问题描述】:
如何制作带有歌曲名称和评分的数组列表?现在我只是在歌曲名称前面放了一个数字,然后使用子字符串来提取评分,但是我需要对评分进行排序并找到评分最高的歌曲 - 使用这种方法非常复杂(在我的世界中) .有什么方法可以链接歌曲和评分(1-5)?我在考虑链表,但如果这是唯一的方法,那对我的技能来说太复杂了。非常欢迎所有建议。提前致谢。
这是我的主要方法:
// Create band
Band Beatles = new Band("Beatles");
// Add musicians
Beatles.musician.add("John Lennon");
Beatles.musician.add("Paul McCartney");
Beatles.musician.add("George Harrison");
Beatles.musician.add("Ringo Starr");
// Add songs (first number is popularity of the song)
Beatles.songs.add("5Yesterday");
Beatles.songs.add("5Let it be");
Beatles.songs.add("3I Saw Her Standing There");
Beatles.songs.add("2Misery");
Beatles.songs.add("4Love Me Do");
//Prints out all songs by Beatles, and their rating
System.out.println(Beatles.musician);
Beatles.getBandSongs();
这是我的乐队课:
import java.util.ArrayList;
public class Band {
public String bandName;
public ArrayList<String> musician = new ArrayList<String>();
public ArrayList<String> songs = new ArrayList<String>();
// Constructor
public Band(String bandName) {
this.bandName = bandName;
}
public void getBandSongs(){
for (String s : songs) {
int rating = Integer.parseInt(s.substring(0,1));
s = s.substring(1);
System.out.println("Rating: " + rating + " - Song name: " + s);
}
}
}
【问题讨论】:
-
标签并没有真正告诉我 - 这是 java 还是 C#?
-
@drew_w:是 java:你可以从 import java 命令中看出!
-
是的。它的 Java 即时学习 :)
-
@AaronB 好眼光 - 我错过了,因为它不在代码部分中
-
无论如何,这里最简单的解决方案是为“Song”创建一个类,该类在类中具有歌曲名称和评分。这些可以添加到数组列表中,而不是使用字符串。