微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

数组索引我们的界限

如何解决数组索引我们的界限

我正在尝试从以下dataset的表中创建2d数组,不幸的是,当我尝试遍历表以将数组值设置为与表中的值相同时,我一直收到出现以下错误

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个值。同样适用于行。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。