我正在尝试将文本文件中的信息存储到数组中,但该项目要求我们通过创建一个临时数组并在我们读取更多文件以容纳新对象时增加其大小来做到这一点。我怎样才能做到这一点?
/**
* reads a csv data file and returns an array of acquaintances
* @param path File path of CSV file
* @return Acquaintances from the file
*/
public static Acquaintance[] read (String path) {
//create an array of acquaintances
Acquaintance[] acqs = new Acquaintance[0];
//open the file
try {
BufferedReader file = new BufferedReader(new FileReader(path));
//read the file until the end
String line;
while( (line = file.readLine()) != null) {
// parse the line just read
String[] parts = line.split(",");
//create an acquaintance object
Acquaintance a = new Acquaintance(parts[0], parts[1], Double.parseDouble(parts[2]));
acqs[0] = a;
//Add the object to the array
//(1) create a new Acquaintance array, with one extra element
Acquaintance[] tmp = new Acquaintance[acqs.length+1];
//(2) copy all old elements into new
Acquaintance[] tmp = acqs.clone();
//(3) assign new Acquaintance object to last element of the array
//(4) assign new array's address to acqs
//for loop
}