【发布时间】:2016-04-20 01:57:35
【问题描述】:
我希望能够从类似于以下内容的文件中读取地图:
0, 0, 0, 0, 0
0, 0, 1, 0, 0
0、1、1、1、1
0, 1, 1, 1, 0
0, 0, 1, 1, 0
并创建一个如下所示的数组列表:
{[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
[0, 0, 1, 1, 0]}
我曾尝试使用 br.readLine(),但它似乎卡住了,但没有在中间抛出错误。
public static int[][] loadFile() 抛出 IOException{
FileReader in = new FileReader(Main.currentFilePath + Main.currentFile); BufferedReader br = new BufferedReader(in); String line; int [] intArray = {}; int [][] fileArray = {}; int j = 0; while ((line = br.readLine()) != null) { List<String> stringList = new ArrayList<String>(Arrays.asList(line.split(","))); String[] stringArray = stringList.toArray(new String[0]); List<Integer> intList = new ArrayList<Integer>(); System.out.println("RRRRR"); for(int i = 0; i < stringList.size(); i++) { Scanner scanner = new Scanner(stringArray[i]); System.out.println("GGGGG"); while (scanner.hasNextInt()) { intList.add(scanner.nextInt()); intArray = intList.parallelStream().mapToInt(Integer::intValue).toArray(); System.out.println("FFFF"); } System.out.println(fileArray[j][i]); fileArray[j][i] = intArray[i]; } j++; } return fileArray; }
【问题讨论】:
-
.... 而
"error in the middle..."是??? -
它抛出
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0,因为您的fileArray的长度为0,您不可能向其中添加任何新行/列 -
@HovercraftFullOfEels 我想你看错了,我神秘地没有收到任何错误,但是使用 print 语句告诉我程序只是停在 fileArray[j][i] = intArray[i];
-
@MadProgrammer 那么如果我使用数组列表,它应该可以工作吗?
-
这表明您在某处有一个空的 catch 块