- import java.util.*;
- public class Test1 {
- /**
- * 1、 一个ArrayList对象aList中存有若干个字符串元素,
- * 现欲遍历该ArrayList对象,删除其中所有值为"abc"的字符串元素,请用代码实现。
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ArrayList<String> aList = new ArrayList<String>();
- aList.add("abc");
- aList.add("de");
- aList.add("ad");
- aList.add("abc");
- aList.add("cde");
- aList.add("sdf");
- System.out.println("改动之前:");
- sop(aList);
- myRemove(aList);
- System.out.println("改动之后:");
- sop(aList);
- }
- private static void sop(ArrayList<String> aList) {
- // TODO Auto-generated method stub
- // TODO Auto-generated method stub
- Iterator<String> it=aList.iterator();
- while(it.hasNext())
- {
-
- System.out.print(it.next()+" ");
-
- }
- System.out.println();
- }
- private static void myRemove(ArrayList<String> aList) {
- // TODO Auto-generated method stub
-
- for(int x=0;x<aList.size();x++)
- aList.remove("abc");
-
-
-
- }
- }
复制代码 |