且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java Arraylist按索引删除多个元素

更新时间:2023-11-18 22:12:40

你需要记住的一件事是,当你使用 ArrayLists 时,它们是多功能的,比 数组.您可以通过删除整个索引来缩短数组,为其添加索引,并使用 ArrayLists 做美妙的事情.

One thing you need to keep in mind is that when you use ArrayLists that they are meant to be versatile, moreso than Arrays. You can shorten an array by removing an entire index, add an index to it, and do wonderfulness with ArrayLists.

这是一个常见问题,他们没有意识到或记得,当您删除一个值时,ArrayList 索引(或任何正确的复数形式)会重新调整,而 ArrayList 缩短.

This is a common problem with people who do not realize, or remember, that when you remove a value, the ArrayList indexes (or whatever the correct plural is) readjust and the ArrayList shortens.

当试图从 ArrayList 中删除元素时,你应该总是从 ArrayList 的末尾开始.

When attempting to remove elements from an ArrayList, you should always start at the end of the ArrayList.

for(int x = arrayList.size() - 1; x > 0; x--)
{
    arrayList.remove(x);
}

这应该为您提供您正在寻找的功能.查看 ArrayList API 以了解其他可能对您有帮助的方法.

This should provide you with the function that you are looking for. Take a look at the ArrayList API for other methods that may help you.