简体   繁体   中英

Adding value to Arraylist<ArrayList> Resets the other values to that added value

So i have an Arraylist Containing Arraylist. Whenever i add a new arraylist to that it will reset all the other arraylists it contains to that arraylist i latest added. I have no idea why this happens ive been trying to figure this out for days...

Heres the arraylist. static ArrayList<ArrayList<BlockPos>> Directions = new ArrayList<ArrayList<BlockPos>>();

Heres the code where it adds the ArrayLists to the 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;
        }
    }
}

So basicly whenever i add a new arraylist to it it just resets all the other arraylists to that value. Anyone have idea why this happens? Or is this a bug in my java or something. (The AStar has nothing to do with the array it just returns an array containing Blockpositions.)

I finally figured out what was the issue. Some corruption in Astar when i call that method it clears an array in the class it has nothing to do with these arrays in this class but somehow after it clears it it also clears that array in this class. Basicly instead of using.clear for that array i just did Array = new Arraylist to clear it that way. So wierd some bug or something. If you have this same issue try locating exactlt where it gets messed up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM