将 List<String[]> 转换为 Array int[][]
Convert List<String[]> to Array int[][]
我需要使用 OpenCSV 库从 csv 文件中读取矩阵。来自 OpenCSV return List String[] 的函数 readAll(),而我需要 int[][]。这是我的:
cSVFileReader = new CSVReader(new FileReader(path), ',');
List<String[]> allRows = cSVFileReader.readAll();
for(String[] row : allRows){
for (int i = 0; i<cSVFileReader.getLinesRead(); i++){
String[] numbers = row[i].split(" ");
int [] ary = new int[numbers.length];
int j = 0;
for (String number : numbers){
ary[j++] = Integer.parseInt(number);
}
}
}
这是输出:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at strassenalgorithm.CSVFile.readM(CSVFile.java:37)
at strassenalgorithm.Main.main(Main.java:26)
当您看到NumberFormatException
时,您需要判断是您的输入有误,还是您的代码有误。
如果您的输入有误,您需要添加在顶层产生 nice-looking 错误的代码,例如
try {
parseMyFile();
} catch (NumberFormatException nfe) {
System.err.println("File contains invalid numbers: "+nfe.getMessage());
}
如果您想允许此输入,例如因为可以用空字符串代替数字,检查特定输入,或在循环内捕获 NumberFormatException
:
for (String number : numbers){
if (number.length() != 0) {
ary[j++] = Integer.parseInt(number);
} else {
ary[j++] = 0; // In this case, zero is the same as "empty"
}
}
在解决输入错误的问题后,我的代码仍然无效,这次给了我 ArrayIndexOutOfBoundsException。也许smn也会有同样的问题,所以这是我的解决方案。
private static int r;
public static int[][] readM(String path) throws FileNotFoundException, IOException {
cSVFileReader = new CSVReader(new FileReader(path), ',');
List<String[]> allRows = cSVFileReader.readAll();
String[][] array = new String[allRows.size()][];
for (int i = 0; i < allRows.size(); i++) {
String[] row = allRows.get(i);
array[i] = row;
h = row.length;
r++;
}
int[][] mResult = new int[r][h];
for (int i =0; i<r; i++) {
for (int j = 0; j< h; j++) {
mResult[i][j] = Integer.parseInt(array[i][j]);
}
}
return mResult; }
表示我的方法有误。我所需要的只是将字符串列表转换为二维字符串数组,然后是二维数组 int
我需要使用 OpenCSV 库从 csv 文件中读取矩阵。来自 OpenCSV return List String[] 的函数 readAll(),而我需要 int[][]。这是我的:
cSVFileReader = new CSVReader(new FileReader(path), ',');
List<String[]> allRows = cSVFileReader.readAll();
for(String[] row : allRows){
for (int i = 0; i<cSVFileReader.getLinesRead(); i++){
String[] numbers = row[i].split(" ");
int [] ary = new int[numbers.length];
int j = 0;
for (String number : numbers){
ary[j++] = Integer.parseInt(number);
}
}
}
这是输出:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at strassenalgorithm.CSVFile.readM(CSVFile.java:37)
at strassenalgorithm.Main.main(Main.java:26)
当您看到NumberFormatException
时,您需要判断是您的输入有误,还是您的代码有误。
如果您的输入有误,您需要添加在顶层产生 nice-looking 错误的代码,例如
try {
parseMyFile();
} catch (NumberFormatException nfe) {
System.err.println("File contains invalid numbers: "+nfe.getMessage());
}
如果您想允许此输入,例如因为可以用空字符串代替数字,检查特定输入,或在循环内捕获 NumberFormatException
:
for (String number : numbers){
if (number.length() != 0) {
ary[j++] = Integer.parseInt(number);
} else {
ary[j++] = 0; // In this case, zero is the same as "empty"
}
}
在解决输入错误的问题后,我的代码仍然无效,这次给了我 ArrayIndexOutOfBoundsException。也许smn也会有同样的问题,所以这是我的解决方案。
private static int r;
public static int[][] readM(String path) throws FileNotFoundException, IOException {
cSVFileReader = new CSVReader(new FileReader(path), ',');
List<String[]> allRows = cSVFileReader.readAll();
String[][] array = new String[allRows.size()][];
for (int i = 0; i < allRows.size(); i++) {
String[] row = allRows.get(i);
array[i] = row;
h = row.length;
r++;
}
int[][] mResult = new int[r][h];
for (int i =0; i<r; i++) {
for (int j = 0; j< h; j++) {
mResult[i][j] = Integer.parseInt(array[i][j]);
}
}
return mResult; }
表示我的方法有误。我所需要的只是将字符串列表转换为二维字符串数组,然后是二维数组 int