快試試...
public class Test {
static class Student {
String id;
String name;
List<Integer> grades;
@Override
public String toString() {
return "ID='" + id + "', name='" + name + "', grades=" + grades + "\n";
}
}
public static void main(String[] args) throws IOException {
String file = "src/main/resources/data.dat";
List<Student> students = new ArrayList<Student>();
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
String[] items = line.split(" ");
Student student = new Student();
student.id=items[0];
student.name=items[1];
if (2 < items.length) {
List<Integer> grades = new ArrayList<Integer>();
for(int i=2; i<items.length;i++) {
grades.add(Integer.valueOf(items[i]));
}
student.grades = grades;
}
students.add(student);
}
br.close();
System.out.println(students);
}
}
輸出:
[ID='1023910', name='jhon', grades=[40, 90, 10, 85]
, ID='1023910', name='jhon', grades=[40, 90, 10, 85]
, ID='1291009', name='max', grades=[89, 92, 49, 100]
, ID='1102293', name='Michael', grades=[20, 100, 82, 72]
]
是文件的那些個行的一部分,或者只是試圖格式化爲計算器文件? – Eric
另外,很確定你想讓我們解釋它,就像我們想要一個白癡,因爲解釋它像一個白癡對每個人都是無益的。 – Eric
您確定要「雙」而不是「雙」嗎? – Eric