[英]print a jagged array from a file
我正在嘗試打印一個鋸齒狀數組,就像我的 txt 文件一樣。 我已經找到了我被告知現在只使用行創建二維數組的行,因為我們還不知道列,然后找到文件中的雙精度數。
double[][] arr = new double[rows][];
Scanner scanner2 = new Scanner (file);
int i = 0;
while (scanner2.hasNextLine()){
String line = scanner2.nextLine();
Scanner newLine = new Scanner(newLine);
int countDoubles = 0;
while(scanner2.hasNextDouble()){
countDoubles++;
}
double[] doubleArr = new double[countDoubles];
newLine = new Scanner(newLine);
// populate doubleArr using newLine
arr[i]=doubleArr;
i++;
}
//how I counted the col on a normal 2d array file
String in = scanner.nextLine();
String [] out = in.split(" ");
for (int s = 0; s < out.length; s++){
col++;
//printing the array
for(int k = 0; k < rows; k++) {
for(int j = 0; j < col; j++) {
arr[i][j] = scanner2.nextDouble();
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
主要存在三個問題,首先,所有列代碼都需要在while循環中為go,以便對文件的每一行都完成。 其次,您在這里陷入了循環while(scanner2.hasNextDouble()){
,最后,我在下面更正了一堆代碼錯誤,但具體來說,您不能將 Scanner 引用到自身Scanner newLine = new Scanner(newLine);
它應該引用上一行的字符串Scanner newLine = new Scanner(line);
這是一個使用盡可能多的代碼的工作示例:
Scanner scanner2 = new Scanner(file);
//Get total lines
int count = 0;
while (scanner2.hasNextLine()) {
count++;
scanner2.nextLine();
}
//Create the array with the correct length
double[][] arr = new double[count][];
//Reset the scanner:
scanner2 = new Scanner(file);
int i = 0;
//Loop thorugh the file one line at a time
while (scanner2.hasNextLine()){
//Get the current line
String line = scanner2.nextLine();
//Changed this line to correctly reference "line", not "newLine"
//Note that you don't need to use scanner here, you could just split the line and use a for loop to go through it
Scanner newLine = new Scanner(line);
int countDoubles = 0;
//Changed this line to reference the correct scanner "newLine"
while(newLine.hasNextDouble()){
countDoubles++;
//Consume the double so that the code advances, otherwise it will just loop for ever
newLine.nextDouble();
}
double[] doubleArr = new double[countDoubles];
//This line vas incorrectly referring to the wrong thing, it should be "line", not "newLine"
newLine = new Scanner(line);
//Use a "for" loop here inside of the while loop to store the column data per each line of the file
for (int y = 0; y < countDoubles; y++)
{
doubleArr[y] = newLine.nextDouble();
}
// populate doubleArr using newLine
arr[i]=doubleArr;
i++;
}
//Code that deals with data needs to be inside the while loop, see the edits above
//String in = scanner.nextLine();
//String [] out = in.split(" ");
//for (int s = 0; s < out.length; s++){
// col++;
//
////printing the array
//for(int k = 0; k < rows; k++) {
// for(int j = 0; j < col; j++) {
// arr[i][j] = scanner2.nextDouble();
// System.out.print(arr[i][j] + " ");
// }
// System.out.println();
//}
//Now we can print the array dynamically by using the array length like so "k < arr.length" and "j < arr[k].length"
for (int k = 0; k < arr.length; k++)
{
for (int j = 0; j < arr[k].length; j++)
{
System.out.print(arr[k][j] + " ");
}
System.out.println();
}
通讀代碼注釋,您應該可以更好地理解它是如何工作的。
使用上面的代碼,我得到以下 output 與我制作的示例文件匹配:
1.0 2.0 3.0
4.0 5.0
6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0
14.0 15.0 16.0 17.0
18.0 19.0
20.0
21.0 22.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.