我正在使用这种方法来获取 Spotify zipfsong 拼图的输入:https ://www.spotify.com/us/jobs/tech/zipfsong/
public static ArrayList<ArrayList<String>> fileReader() throws IOException {
Scanner scanner = new Scanner(System.in);
ArrayList<ArrayList<String>> fileContents = new ArrayList<ArrayList<String>>();
try{
String currentLine = scanner.nextLine();
ArrayList<String> fileRow = new ArrayList(Arrays.asList(currentLine.split(" ")));
fileContents.add(fileRow);
int numberOfInputLines = Integer.parseInt(fileRow.get(0));
int numberOfOutputLines = Integer.parseInt(fileRow.get(1));
if (numberOfInputLines < numberOfOutputLines) {
return null;
}
for (int lineCount = 0; lineCount < numberOfInputLines; lineCount++) {
currentLine = scanner.nextLine();
fileRow = new ArrayList(Arrays.asList(currentLine.split(" ")));
fileContents.add(fileRow);
}
} catch(Exception e) {
System.out.println("Read Error");
return null;
} finally {
scanner.close();
}
return fileContents;
}
所以,我基本上阅读了第一行,找到接下来的行数,然后使用 Scanner 阅读它们。现在,出于某种原因,我不断收到 ArrayIndexOutOfBoundsException。是因为这个输入法吗??