我必须填充一些对象的成员,但我不知道它们有多少。这就是我使用 ArrayList 的原因,因为它是动态大小的。但我不知道如何在 ArrayList 中填充这些对象。我从文件中逐行读取,如果找到匹配模式,我必须创建新对象并用数据填充它。
//read data from file to BufferedReader, that we can read out single line by line
BufferedReader mBufferedReader = new BufferedReader(new FileReader(mFile));
String line;
while ((line = mBufferedReader.readLine()) != null) {
//pattern "name" for searching points
Pattern pattern = Pattern.compile("\"(.*?)\"");
//array of delimited Strings separated with comma
String[] delimitedStrings = line.split(",");
//if we find "name" of point, get code, lat and lon of that point
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
String name = delimitedStrings[0];
mData.add(new myData().name = name);
String code = delimitedStrings[1];
mData.add(new myData().code = code);
}
}
myData 类有成员字符串名称,例如字符串代码。我需要使用 add 方法的类似的东西,但这不起作用。谢谢!