如何填充ListBuffer [List [Any]](Scala)?

问题描述 投票:1回答:1

我有一个像这样声明的ListBuffer:

var distances_buffer: ListBuffer[List[Any]] = ListBuffer.empty[List[Any]]

而且我正在尝试用这样的数据填充它:

for(current <- 0 to training_list_length - 1){
   //A and B are examples
   distances_buffer(current) ++= List[Any](A,B)
}

但是出现以下错误:

java.lang.IndexOutOfBoundsException:0

我想念什么?

编辑!更多信息:

我有一个要点及其类别的列表(名为:training_list)。 (x,y,class):

training_list : List[((Double, Double, String))]

我也有一个额外的点,给出了x和y值。

我的目标是从训练列表中的每个点计算额外点的欧式距离,并创建一个如下所示的结果:

//example
List((x: Double, y: Double, class: String), distance: String)

List((4.3,3.0,Iris-setosa), 1.2529964086141665), (4.4,3.0,Iris-setosa), 1.341640786499874)...

如您所见,我想在列表中包括点的坐标(来自training_list),点的类以及距离。

for(current <- 0 to training_list_length - 1){
    val dist = eDistance(test_x, test_y, training_x(current), training_y(current))
    distances_buffer += ListBuffer[Any](training_result(current),dist)
  }

创建此列表后,我想根据距离对它进行排序。也卡在这里!

scala list append indexoutofboundsexception listbuffer
1个回答
0
投票
根据路易斯的建议,如果可能,应避免使用Anyvar,因此以下示例可能会促使您考虑使用其他方法
© www.soinside.com 2019 - 2024. All rights reserved.