我需要将文本文件读入2D数组,我可以将文件完全正确地读入程序(参见下面的代码)但是我无法理解如何将它们读入2D数组。函数读入的数组是一个全局数组,因此它不在函数中。
此外,我还不知道数组最初的行数(当前设置为300,因为它不会超过这个),我知道这可能会导致问题,我已经知道了看到一些人建议使用ArrayLists但是我必须有一个2D数组,所以我也想知道是否有办法将ArrayList更改为2D数组,如果这样更有效?
public static String readMaze(String fileName) {
String line = null;
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
for (int i = 0; i < mazeNew.length; i++) {
for (int j = 0; j < mazeNew[i].length; j++) {
// mazeNew[i][j] = ; - this is where I think something needs to be added
}
}
}
bufferedReader.close();
}
catch (FileNotFoundException ex) {
System.out.println("Unable to open file: " + fileName);
}
catch (IOException ex) {
System.out.println("Error reading file: " + fileName);
}
return fileName;
}
示例文本文件:
11 4
5 6
4 6
0 5
3 5
8 7
1 4
答案 0 :(得分:1)
这里有一些选项,但通常你会想要使用Java Scanner class,因为它专门针对这类事情而设计。或者,使用现有的结构化数据格式(如JSON或XML)和现有的解析器一起使用 - 这样做的好处是可以利用大量的工具和库来处理这些格式并且不具备这些格式。重新发明任何东西。
然而,通过扫描仪方法,它会是这样的:
public static ArrayList<int[]> readMaze(String fileName) {
// Number of ints per line:
int width=2;
// This will be the output - a list of rows, each with 'width' entries:
ArrayList<int[]> results=new ArrayList<int[]>();
String line = null;
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
Scanner mazeRunner = new Scanner(bufferedReader);
// While we've got another line..
while (mazeRunner.hasNextLine()) {
// Setup current row:
int[] row = new int[width];
// For each number..
for (int i = 0; i < width; i++) {
// Read the number and add it to the current row:
row[i] = mazeRunner.nextInt();
}
// Add the row to the results:
results.add(row);
// Go to the next line (optional, but helps deal with erroneous input files):
if ( mazeRunner.hasNextLine() ) {
// Go to the next line:
mazeRunner.nextLine();
}
}
mazeRunner.close();
}
catch (FileNotFoundException ex) {
System.out.println("Unable to open file: " + fileName);
}
catch (IOException ex) {
System.out.println("Error reading file: " + fileName);
}
return results;
}
答案 1 :(得分:0)
如果您已修复否。您可以使用此列,但请确保输入文件必须遵循相同的no coulmns。
<p> Please checkout http://stackoverflow.com. It has very cool logo https://d13yacurqjgara.cloudfront.net/users/1249/screenshots/2247671/stackoverflow.png.</p>
<p> Another paragraph https://stackexchange.com. and here is another url I am making up: https://mycoolurlexample.com/this/is/an/example</p>
答案 2 :(得分:0)
下面是核心逻辑,您可能还想处理一些错误,例如分裂的行数,空行等等。
private int zeroPrefixLength(final byte[] bytes) {
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] != 0) {
return i;
}
}
return bytes.length;
}
答案 3 :(得分:0)
这样的东西会起作用
它不会只读取二维文本文件..它应该适用于任何尺寸
public class Utile{
public static ArrayList<int[]> readMaze(String path){
ArrayList<int[]> result = new ArrayList<>();
try{
Scanner sc = new Scanner(new File(path));
String[] temp;
String line;
while(sc.hasNextLine()){
line = sc.nextLine();
if (line.length() != 0){ //if the line is empty it will cause NumberFormatException
temp = line.split(" ");
int[] val = new int[temp.length];
for(int i = 0;i < temp.length;i++){
val[i] = Integer.pareseInt(temp[i]);
}
result.add(val);
}
}
sc.close();
}catch(Exception e){
e.printStackTrace(); //just log it for now
}
return result;
}
}
答案 4 :(得分:-1)
我不是Java专家,但在PHP中我会用 explode()来做。但我找到了一个如何在java中使用string.split()做同样的例子。结果是相同的...内容的2D数组。如果可能,您应该尝试向该文本文档中的行添加分隔符。但是你可以在空格字符上拆分行。
示例:强>
String foo = "This,that,other";
String[] split = foo.split(",");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < split.length; i++) {
sb.append(split[i]);
if (i != split.length - 1) {
sb.append(" ");
}
}
String joined = sb.toString();