0

我试图通过从文件中读取对象来实例化一个对象数组(跑步者)。有六个不同的变量可以描述每个人。我试图弄清楚如何创建一个数组,将这些跑步者的所有相同值存储在同一个元素中,同时保持每个跑步者的各个部分。

该文件如下所示:

{1,Gebre Gebremariam,2:08:00,,Ethiopia,ETH
2,Emmanuel Mutai,2:06:28,,Kenya,KEN
3,Geoffrey Mutai,2:05:06,,Kenya,KEN
4,Tsegaye Kebede,2:07:14,,Ethiopia,ETH
6,Jaouad Gharib,2:08:26,,Morocco,MAR
7,Meb Keflezighi,2:09:13,CA,United States,USA
8,Mathew Kisorio,2:10:58,,Kenya,KEN
10,Viktor Rothlin,2:12:26,,Switzerland,SUI
11,Bobby Curtis,2:16:44,PA,United States,USA
12,Ed Moran,2:11:47,VA,United States,USA
14,Abdellah Falil,2:10:35,,Morocco,MAR
15,Juan Luis Barrios,2:14:10,,Mexico,MEX
18,Stephen Muzhingi,2:29:10,,Zimbabwe,ZIM}
4

2 回答 2

1

创建一个类Runner,该类具有构成单个跑步者的六个字段的属性,然后将您的文件读入 aList<Runner>Runner[].

// if this is a CSV file
List<Runner> runners = new ArrayList<Runner>();
for (String[] line: csvLines){
   Runner r = new Runner();
   r.setName(line[0]);
   r.setAge(Integer.parseInt(line[1]);
   runners.add(r);
}
于 2013-01-25T02:04:13.547 回答
0

下面将一个 Runner 的实例添加到一个 ArrayList 中。您可能希望解析字符串以提供适当的构造函数参数。

List<Runner> runners = new ArrayList<Runner>();

BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String currentLine;

for(int index = 0; (currentLine = bufferedReader.readLine()) != null; index++)
{
    runners.add(new Runner(currentLine));
}

bufferedReader.close();
于 2013-01-25T02:06:49.897 回答