从ArrayList中删除多个项目 [英] Remove multiple items from ArrayList

查看:54
本文介绍了从ArrayList中删除多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要从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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆