我需要将数组转换为数组列表并且不确定如何执行此操作,我已经查看了其他文章,如果有人可以提供给我的话,我似乎无法使用它指针以及如何将其转换为将成为fab的数组列表。
import java.util.*;
public class SearchExamResults
{
public static void main(String[] args)
{
Result[] resultsArray = new Result[20];
input(resultsArray);
sortByMark(resultsArray);
output(resultsArray);
}
public static void input(Result[] array)
{
Scanner kybd = new Scanner(System.in);
for (int i = 0; i < array.length; i++)
{
System.out.println("Candidate " + (i + 1));
System.out.print("\tName: ");
String name = kybd.nextLine();
System.out.print("\tMark: ");
int mark = kybd.nextInt();
kybd.nextLine(); //to flush the kybd buffer
array[i] = new Result(name, mark);
}
}
public static void sortByMark(Result[] array)
{
//selection sort
for (int pass = 1; pass < array.length; pass++)
{
int lastElement = array.length - pass;
int largePos = findLargestMark(array, array.length - pass);
if (largePos != array.length - pass)
{
Result temp = array[largePos];
array[largePos] = array[lastElement];
array[lastElement] = temp;
}
//this is for testing purposes only
//output(arr);
}
}
public static int findLargestMark(Result[] array, int last)
{
int largestPos = 0;
for (int i = 1; i <= last; i++)
{
if (array[i].getMark() > array[largestPos].getMark())
{
largestPos = i;
}
}
return largestPos;
}
public static void output(Result[] array)
{
System.out.println("\nExam results");
System.out.println("============");
for (int i = 0; i < array.length; i++)
{
System.out.printf("\t%-20s\t%d\n", array[i].getName(), array[i].getMark());
}
}
}
答案 0 :(得分:1)
您可以使用Arrays.asList(T...)
,也可以通过迭代所有元素来自行完成:
ArrayList<Result> list = new ArrayList<Result>();
for(Result r : resultsArray) {
list.add(r);
}
PS:您可以使用搜索功能查看之前是否曾询问过这个问题。