数组索引超出范围

huangapple go评论67阅读模式
英文:

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(&quot;/data/winequalityN.csv&quot;, &quot;header&quot;); //Importing our dataset
println(table.getRowCount() + &quot; total rows in table&quot;); //Print the number of rows in the table
println(table.getColumnCount() + &quot; total columns in table&quot;); //Print the number of columns in the table

for (int i = 0; i &lt; table.getColumnCount(); i++){
  for (int j = 0; j &lt; 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 &lt; table.getColumnCount(); i++){
  for (int j = 0; j &lt; 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 &lt; table.getColumnCount()-1; i++){ 
      for (int j = 0; j &lt; 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.

huangapple
  • 本文由 发表于 2020年10月5日 06:13:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64200381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定