Java中是否有Oracle官方实现的按索引批量删ArrayList元素的单命令方法?
Hey there!
Unfortunately, there is no single built-in method provided by Oracle's Java standard library that directly accepts a list of indices and deletes all corresponding elements from an ArrayList in one command.
The core ArrayList class only offers these delete-related methods out of the box:
remove(int index): Deletes the element at a single specified index.remove(Object o): Deletes the first occurrence of a specified object.removeRange(int fromIndex, int toIndex): Deletes elements in a contiguous range (note this is a protected method, so you can't call it directly on a standardArrayListinstance unless you subclass it).removeIf(Predicate<? super E> filter): Deletes elements that match a predicate, but this works based on element values, not indices.
Since your use case involves arbitrary, non-contiguous indices, none of these methods handle it in a single call. You'll have to implement the logic yourself—either by iterating over the index list (remember to iterate in reverse order to avoid index shifting issues!) or wrapping that logic into a helper method.
As you mentioned, you can condense the iteration into a one-liner using streams, but that's still custom code, not an official single command.
内容的提问来源于stack exchange,提问作者Azog the Debugger

