嘿,伙计们和姑娘们。
背景:我正在开发一个要求 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);
}
}