java - 数组索引我们的边界
问题描述
我正在尝试从以下数据集的表创建一个二维数组,不幸的是,当我尝试遍历我的表以将数组值设置为与表的值相同时,我不断收到以下错误:
6497 total rows in table
13 total columns in table
ArrayIndexOutOfBoundsException: Column 13 does not exist.
以下是我的代码。顺便说一下,我正在使用 java 模式下的处理。
Table table;
float[][] variablesDataframe = new float[12][6497];
table = loadTable("/data/winequalityN.csv", "header"); //Importing our dataset
println(table.getRowCount() + " total rows in table"); //Print the number of rows in the table
println(table.getColumnCount() + " total columns in table"); //Print the number of columns in the table
for (int i = 0; i < table.getColumnCount(); i++){
for (int j = 0; j < table.getRowCount(); j++){
variablesDataframe[i][j] = table.getFloat(i + 1, j);
}
}
我跳过第一列 (i + 1) 的原因是因为它是 dtype 字符串/对象,我只想要浮点数,它是我的 2d 数组中数据集的其余部分。
如果有人可以帮助我实现这一目标或修复代码,将不胜感激。
干杯。
解决方案
您的 variableDataframe 数组定义为 [12][6497]
在您的代码中,您的第一个 for 循环将 i 从 0 初始化,它将继续迭代直到达到 13。因此,您总共将获得 13 个 i 值。
for (int i = 0; i < table.getColumnCount(); i++){
for (int j = 0; j < table.getRowCount(); j++){
variablesDataframe[i][j] = table.getFloat(i + 1, j);
}
}
由于您想跳过第一行,请改为执行此操作
for (int i = 0; i < table.getColumnCount()-1; i++){
for (int j = 0; j < table.getRowCount()-1; j++){
variablesDataframe[i][j] = table.getFloat(i+1, j);
}
}
这将确保您跳过第一行,并且数组中只有 12 个值。同样适用于行。
推荐阅读
- unix - Unix:带分隔符的分割线
- python - 如何使用 matplotlib 在 2D 磁盘旁边绘制 3D 球体
- tomcat - 如何使 TOMCAT 只能访问本地主机,除了一个应用程序
- python - 无法导入文件
- ansible - if语句的ansible剧本中的多个命令?
- php - 如何向上舍入到非零阈值
- python - Flask Web 应用程序无法在 Heroku 上运行,但可以在 localhost 上正常运行
- android - 在 recyclerview 适配器中获取点击的项目
- html - 如何使用 flex 模型在右侧移动按钮?
- python - 如何通过 POST 请求正确使用 Python 请求进行身份验证