gpt4 book ai didi

java - ArrayList向第三层添加元素

转载 作者:行者123 更新时间:2023-12-01 21:13:16 28 4
gpt4 key购买 nike

我正在循环遍历数组列表并尝试添加从 ai.get(k).add(Cent.get(k).get(m)-Cent.get(k).get 获得的每个答案(l));进入相应的数组列表的数组列表的数组列表。对于每三个元素,它应该将以下元素添加到新的数组列表中。该数据集中有 12 个元素,因此它应该看起来像 [[[0,1,2],[3,4,5],[6,7,8],[9,10,11]],[[] ,[],[],[]],[[],[],[],[]]]

ArrayList<ArrayList> ai = new ArrayList<ArrayList>();
// Creates arrays based on number of centroids
for (int k = 0; k < numCen; k++) {
ai.add(new ArrayList(k));
}

for (int i = 0; i < numCen; i++) {
for(int k = 0; k < dim; k++) {
ai.get(i).add(new ArrayList(k));
//ai.add(new ArrayList(i));
}
}

System.out.println(ai);


// Gets all of the centroids
for (int k = 0; k < numCen; k++) {
// Gets the centroid itself
for (int m = 0; m < Cent.size() + 1; m++) {
// Gets all the elements in array
for (int l = 0; l < Cent.size() + 1; l++) {
// Skips over the element being subtracted so you never get 0
if(Cent.get(k).get(m) == Cent.get(k).get(l)) {
continue;
}
//Places all the elements in ai
ai.get(k).add(Cent.get(k).get(m)-Cent.get(k).get(l));
}
}
System.out.println(ai);
}

这是我当前的输出:

[[[], [], [], [], -0.4861111111108659, 0.18691148775888072, 0.18055555555552283, 0.4861111111108659, 0.6730225988697466, 0.6666666666663887, -0.18691148775888072, -0.6730225988697466, -0.006355932203357881, -0.18055555555552283, -0.6666666666663887, 0.006355932203357881], [[], [], [], [], 0.027777777777828083, -0.2504708097928492, -0.2638888888887171, -0.027777777777828083, -0.2782485875706773, -0.29166666666654517, 0.2504708097928492, 0.2782485875706773, -0.013418079095867896, 0.2638888888887171, 0.29166666666654517, 0.013418079095867896], [[], [], [], [], -0.40277777777757906, 0.15442561205268038, 0.1805555555555111, 0.40277777777757906, 0.5572033898302594, 0.5833333333330901, -0.15442561205268038, -0.5572033898302594, 0.02612994350283071, -0.1805555555555111, -0.5833333333330901, -0.02612994350283071]]

预期输出:

[[[-0.4861111111108659, 0.18691148775888072, 0.18055555555552283], 
[0.4861111111108659, 0.6730225988697466, 0.6666666666663887],
[-0.18691148775888072, -0.6730225988697466, -0.006355932203357881],
[-0.18055555555552283, -0.6666666666663887, 0.006355932203357881]],
[[0.027777777777828083, -0.2504708097928492, -0.2638888888887171],
[-0.027777777777828083, -0.2782485875706773, -0.29166666666654517],
[0.2504708097928492, 0.2782485875706773, -0.013418079095867896],
[0.2638888888887171, 0.29166666666654517, 0.013418079095867896]], [[
-0.40277777777757906, 0.15442561205268038, 0.1805555555555111],
[0.40277777777757906, 0.5572033898302594, 0.5833333333330901],
[-0.15442561205268038, -0.5572033898302594, 0.02612994350283071],
[-0.1805555555555111, -0.5833333333330901, -0.02612994350283071]]]

最佳答案

您添加了第三层。目前你只有两个。

  1. List<Integer>可以存储[1,2]
  2. List<List<Integer>>可存储[[1,2],[3,4]] ,这就是你现在拥有的
  3. List<List<List<Integer>>>可以存储[ [[1,2],[3,4]] , [[5,6],[7,8]] ]

数量List<>彼此包装在一起的数量是层数(或维度,这是一个通用名称)。
当然 List 中的任何一个s 可以为空,[]

<小时/>哦,好吧,是的。
所以你的内心 ArrayList包含通用 Object -s,你碰巧填写了 ArrayList s,但仍然很难访问它们,您可以这样做:

((ArrayList)ai.get(firstIndex).get(secondIndex)).get/set(thirdIndex)

这在你的代码中没有发生,所以我有点困惑为什么你会在第三层有任何东西。这是你根本没有表现出来的东西。

无论如何,您确实应该切换到“更多类型”的方法,并且已经

List<List<List<Float>>> ai=new ArrayList<>();

在开头。

关于java - ArrayList向第三层添加元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58892584/

28 4 0