英文:
Array Index our of bounds
问题
我试图从以下数据集的表格中创建一个2D数组,但不幸的是,当我尝试迭代我的表格以将数组值设置为与表格相应位置的值相同时,我一直收到以下错误:
表格中的总行数为6497
表格中的总列数为13
ArrayIndexOutOfBoundsException: 列13不存在。
以下是我的代码。顺便说一下,我正在使用Java模式中的Processing。
Table table;
float[][] variablesDataframe = new float[12][6497];
table = loadTable("/data/winequalityN.csv", "header"); //导入我们的数据集
println(table.getRowCount() + "表格中的总行数"); //打印表格中的行数
println(table.getColumnCount() + "表格中的总列数"); //打印表格中的列数
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)的原因是因为它的数据类型是字符串/对象,而我只想要浮点数,这是我的2D数组中的其余数据集。
如果有人能帮助我实现这个目标或修复代码,将不胜感激。
谢谢。
英文:
I am trying to create a 2d array from a table of the following dataset, unfortunately when I try iterate through my table to set the array values to be the same as those of the table accordingly I keep receiving the following error:
6497 total rows in table
13 total columns in table
ArrayIndexOutOfBoundsException: Column 13 does not exist.
The following is my code. I am using processing in java mode by the way.
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);
}
}
The reason I skip over the first column (i + 1) is because it's dtype String/Object and I only want the floats which is the rest of the dataset in my 2d Array.
If anyone can help me achieve this or fix the code would be much appreciated.
Cheers.
答案1
得分: 1
你的 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 个值。行也是同样的道理。
英文:
Your variableDataframe array is defined as [12][6497]
In your code, your first for loop initializes i from 0 and it will keep iterating till it hits 13. So, in all you will get 13 values of 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);
}
}
Since you want to skip the first row, do this instead
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);
}
}
This will ensure that you are skipping the first row and you will only have 12 values in the array. Same applies to the rows as well.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论