Ive得到了一個名爲HighScores.txt文件,其中包含球員的名字和點數:如何排序的Java的ArrayList
name1 2
name2 5
name3 1
name4 23
name5 51
而且我的繼承人的代碼讀取該文件的內容,分割每行並將其附加到ArrayList highScores
:
public class fileHandling {
static ArrayList highScores = new ArrayList();
public static void readFile(String[] args) throws Exception {
File file = new File(fileHandling.class.getClassLoader()
.getResource("HighScores.txt").getPath());
Scanner read = new Scanner(file);
while (read.hasNextLine()) {
String line = read.nextLine();
String[] result = line.split("\\s+");
highScores.add(Arrays.toString(result));
System.out.println(highScores);
}
}
}
我如何排序,然後將此ArrayList由點每個玩家有多少?
即因此新數組列表將是:
[[name5, 51], [name4, 23], [name2, 5], [name1, 2], [name3, 1]]
你以前做過什麼研究嗎? – gaborsch