从 ArrayList 中删除多个项目 [英] Remove multiple items from ArrayList
问题描述
我想从数组列表中删除多个项目.
I want to remove multiple items from arraylist.
这是列表:
String1-1, String5, String6, String1-2, String5, String6, String1-3, String5, String6
我想从列表中删除 String1-
.但是因为不是所有的 String1-
都有相同的结尾,所以它们没有被删除.所以 list.remove("String1-")
不起作用.
I want to remove String1-
from the list. But because not all the String1-
have the same end, they are not removed. So list.remove("String1-")
is not working.
问题是:当我想删除的字符串的结尾不一样时,如何从arraylist中删除多个项目.
The question is: How to remove the multiple items from arraylist when the end of the string i want to remove is not the same.
推荐答案
没有单一的方法可以做到这一点,但是如果您迭代 ArrayList
并使用String.startsWith
方法测试字符串是否以"String1-"
开头.
There is no single method that will do this, but you can do it quite easily if you iterate over the ArrayList
and use the String.startsWith
method to test whether the string starts with "String1-"
.
这是一个例子.请注意,我使用了迭代器,因为它比使用 for
循环更容易从数组中删除内容.
Here is an example. Note that I have used an iterator because it makes removing things from arrays much easier than if you use a for
loop.
Iterator<String> it = arrayList.iterator();
while (it.hasNext() ) {
String value = it.next();
if (value.startsWith("String1-") ) {
it.remove();
}
}
这篇关于从 ArrayList 中删除多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!