* 一个ArrayList对象aList中存有若干个字符串元素,
* 现欲遍历该ArrayList对象,删除其中所有值为"abc"的字符串元素,
* 请用代码实现。
public class Demo17 {
public static void main(String[] args) {
ArrayList<String> al = new ArrayList<String>();
String s = "abc"; //需要删除的指定字符串
al.add("abcd");
al.add("abc");
al.add("abcded");
al.add("abcdabc");
ArrayList<String> newList = remove(al,s);
System.out.println(newList);
}
public static ArrayList<String> remove(ArrayList<String> al, String s) {
for(int x = 0; x < al.size(); x ++)
{
if(al.get(x).equals(s))
{
al.remove(al.get(x));
}
}
return al; //返回需要的集合
}
} |
|