【问题标题】:Array Index our of bounds [closed]数组索引超出范围[关闭]
【发布时间】:2025-11-23 12:35:01
【问题描述】:

我正在尝试从以下dataset 的表创建一个二维数组,不幸的是,当我尝试遍历我的表以将数组值设置为与表的值相同时,我不断收到以下错误:

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 数组中数据集的其余部分。

如果有人可以帮助我实现这一点或修复代码,将不胜感激。

干杯。

【问题讨论】:

  • 你能告诉我们你的Table类吗?
  • 嗯,确定吗?您将variablesDataframe 声明为float[12][6497],因此当您尝试将某些内容放入不存在的variablesDataframep[12] 中时(仅存在索引0 到11),您会收到错误消息。在开始循环之前,请确保您的表格和数据框的大小相互匹配。
  • 您的table 有 13 列。 getFloat 是用 i + 1 调用的,所以值的范围是 1 到 13,table 中没有索引 13。同样对于variablesDataframe[i][j],索引i 从 0 变为 12,但该数组中没有索引 12
  • 他想忽略第一个表的列。你的i for 循环需要像i &lt; table.getColumnCount() -1,这样当你调用table.getFloat(i + 1, j)时,它不会溢出

标签: java arrays dataframe loops processing


【解决方案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 个值。这同样适用于行。

【讨论】:

  • 是的,我明白你的意思。但是,当我使用您编写的代码时,我仍然会遇到同样的错误。我正在使用处理可能与此有关?
  • 什么样的处理?
  • 处理 3.5.4,但是 nvm 我和老师一起解决了。无论如何,感谢您提供的所有帮助。