向数组列表添加值 将<ArrayList>其他值重置为该附加值



所以我有一个包含数组列表的数组列表。每当我添加一个新的数组列表时,它都会将它包含的所有其他数组列表重置为我最近添加的数组列表。我不知道为什么会发生这种事我已经想了好几天了。。。

这是数组列表。static ArrayList<ArrayList<BlockPos>> Directions = new ArrayList<ArrayList<BlockPos>>();

这是它将ArrayList添加到ArrayList的代码。

public static void CalculateEveryPath() {
//Calculate path to every direction.
BlockPos Start = new BlockPos(mc.player.posX, mc.player.posY, mc.player.posZ);
BlockPos Goal = null;
int ArrayDir = 0;

if (Dir1 == false) {
//+X
Goal = new BlockPos(Start.add(350, 0, 0));
ArrayDir = 1;
} else if (Dir2 == false) {
//-X
Goal = new BlockPos(Start.add(-350, 0, 0));
ArrayDir = 2;
} else if (Dir3 == false) {
//+Z
Goal = new BlockPos(Start.add(0, 0, 300));
ArrayDir = 3;
} else if (Dir4 == false) {
//-Z
Goal = new BlockPos(Start.add(0, 0, -350));
ArrayDir = 4;
}

if (Goal != null) {
ArrayList<BlockPos> Dir = new ArrayList<BlockPos>();
Dir = AStar.GetPath(Start, Goal, 350, false, true);
Dir = AStar.GetPath(Start, AStar.Closest, 450, false, true);
if (Dir.isEmpty()) {
Dir.add(Start);
}

if (ArrayDir == 1) {
Directions.add(Dir);
Renderer.PositionsYellow.addAll(Directions.get(0));
Dir1 = true;
} else if (ArrayDir == 2) {
//Adds Dir array to Directions. BUT RESETS THE ALL THE OTHER ARRAYS TO THAT VALUE!
Directions.add(Dir);
Renderer.PositionsYellow.addAll(Directions.get(1));
Dir2 = true;
} else if (ArrayDir == 3) {
//Adds Dir array to Directions. BUT RESETS THE ALL THE OTHER ARRAYS TO THAT VALUE!
Directions.add(Dir);
Renderer.PositionsYellow.addAll(Directions.get(2));
Dir3 = true;
} else if (ArrayDir == 4) {
//Adds Dir array to Directions. BUT RESETS THE ALL THE OTHER ARRAYS TO THAT VALUE!
Directions.add(Dir);
Renderer.PositionsYellow.addAll(Directions.get(3));
Dir4 = true;
}
}
}

所以基本上,每当我添加一个新的数组列表时,它只会将所有其他数组列表重置为该值。有人知道为什么会发生这种事吗?或者这是我的java中的一个bug还是什么的。(AStar与数组无关,它只是返回一个包含Blockpositions的数组。(

我终于弄清楚问题出在哪里了。Astar中的一些损坏——当我调用该方法时,它会清除类中的一个数组——它与该类中的这些数组无关,但不知何故,在它清除它之后,它也会清除此类中的那个数组。基本上,我没有对那个数组使用.clear,而是用array=new Arraylist来清除它。所以有个虫子什么的。如果你有同样的问题,试着准确定位它被搞砸的地方。

最新更新