嘿,夥計們和gals。排序多個ArrayList建議?
背景: 我正在做一個高分計劃,要求5個名字和5個分數的作業。輸入相應分數的名稱後,程序按照最高分數對兩個ArrayList進行排序。最後,它按排序順序顯示他們的分數。
問題: 我有一段時間試圖排序ArrayLists的魔鬼,你有任何排序ArrayLists的建議嗎?
代碼:
import java.util.*;
public class Assignment6
{
public static void main(String args[])
{
ArrayList<String> names = new ArrayList();
ArrayList<Integer> scores = new ArrayList();
initializeArrays(names, scores);
//sortArrays(names, scores);
displayArrays(names, scores);
}
public static void initializeArrays(ArrayList names, ArrayList scores)
{
Scanner in = new Scanner(System.in);
for(int i=0; i<5; i++)
{
System.out.println("Enter the name for score # " + (i+1) + ": ");
names.add(in.next());
System.out.println("Enter the score for score # " + (i+1) + ": ");
scores.add(in.next());
}
}
public static void sortArrays(ArrayList names, ArrayList scores)
{
for(int i=0; i<5; i++)
{
if(scores[i] < scores[i+1])
{
Collections.swap(scores,a, b);
Collections.swap(names,a, b);
}
}
}
public static void displayArrays(ArrayList names, ArrayList scores)
{
System.out.println("Top Scorers: ");
System.out.println(names);
System.out.println(scores);
}
}
你有什麼問題? –
Collections.swap()是一個方便的工具,爲您的目的!但是'a'和'b'是什麼?它們沒有在任何地方定義,編譯器消息應該儘可能多地叫喊。 – clwhisk