gpt4 book ai didi

java - 将表中的 float 解析为二维数组

转载 作者:行者123 更新时间:2023-11-30 03:58:09 28 4
gpt4 key购买 nike

我有一个包含 float 方 table 的文件。该文件是一个示例,因此读取的其他文件中的行数和列数可能会发生变化。

我遇到了越界异常,但无法找出问题所在。

 while ((line=bufferedReader.readLine())!=null){
String[] allIds = line.split(tabSplit);
String[] allFloats = new String[allIds.length-2];
//i do "length-2" because the first two columns in the table are not numbers and are
supposed to be ignored.

System.arraycopy(allIds, 2, allFloats, 0, allIds.length-2);
int rows = rowsNumber;
int cols = colsNumber;
//i have "rows" and "cols" values already extracted from the file and they return the
correct integers.

double [][]twoD = new double[rows][cols];
int rowCount = 0;
for (int i = 0; i<rows; i++) {
twoD[rowCount][i] = Double.parseDouble(allFloats[i]);
}
rowCount++;
}

}

我的表格看起来像这样,但有更多的行和列:

#18204     name01     2.67   2.79   2.87   2.7
#12480 name02 4.01 3.64 4.06 4.24
#21408 name03 3.4 3.55 3.34 3.58
#a2u47 name04 7.4 7.52 7.62 7.23
#38590 name05 7.63 7.29 8 7.72

当我打印 allFloats 时,它正确返回单独数组中的每一行。我不明白为什么当我尝试创建二维数组时会出现错误。

最佳答案

编辑:请尝试以下操作:

int rowCount = 0;
int rows = rowsNumber;
int cols = colsNumber;
double[][] twoD = new double[rows][cols];

while ( ( line=bufferedReader.readLine() ) != null )
{
String[] allIds = line.split( tabSplit );
String[] allFloats = new String[allIds.length-2];

System.arraycopy(allIds, 2, allFloats, 0, allIds.length-2);

for (int i = 0; i<cols; i++)
{
twoD[rowCount][i] = Double.parseDouble(allFloats[i]);
}

rowCount++
}

关于java - 将表中的 float 解析为二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22598451/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com