从arraylist中删除对象而不更改其他对象的索引

Zag*_*yax 1 java indexing arraylist

所以我需要在索引号下删除一个特定对象,该索引号是我的循环在arraylist中传递了多少次.

假设我想删除arraylist上索引为0的对象

但索引1和索引2(依此类推)上的对象仍然需要与删除索引0之前的索引号相同.

        for (int i = 0; i < 4 i++) {
            player thisPlayer = players.get(i);

            if (not important) {
                players.remove(thisPlayer);
            }
        }
Run Code Online (Sandbox Code Playgroud)

如果需要移除玩家1,则其他玩家需要保持相同的索引.

我该怎么办?

小智 6

而不是使用

players.remove(thisPlayer);
Run Code Online (Sandbox Code Playgroud)

你可以试试一下

players.set(players.indexOf(thisPlayer),null);
Run Code Online (Sandbox Code Playgroud)