将 CSV 文件读入链表 (Java)



所以我在读取 CSV 文件并将其添加到我的链表中时遇到问题。我已经将csv文件读取到数组,数组列表之类的东西中,但从未读取过单链表。现在我的问题是,我在解析薪水的代码行上不断收到一个 ArrayOutOfBoundsException。该代码是我的菜单类的一部分,我在其中读取文件并将其添加到链表中。所以我的问题是,如何正确地将 csv 文件读入链表?谢谢!

public class EmpMenu {
private SLList<Employee> list = new SLList<Employee>();
public EmpMenu() {
Scanner inFile = null;
try {
inFile = new Scanner(new File("employees.txt"));
} catch (FileNotFoundException x) {
System.err.print("File not found");
}
while (inFile.hasNextLine()) {
String line = inFile.nextLine();
String[] records = line.split(",[ ]");
String employeeID = records[0];
String fName = records[1];
String lName = records[2];
Double salary = Double.parseDouble(records[3]);
Employee employee = new Employee(employeeID, fName, lName, salary);
list.add(employee);
}

您的问题似乎与LinkedList无关,ArrayOutOfBoundsException表明该问题与将line拆分records字符串数组正确相关,records[3]不存在。因此,请检查records的长度,您会发现它有什么问题。