黑马程序员技术交流社区

标题: 求解惑 [打印本页]

作者: 田宇鹤    时间: 2012-12-10 23:48
标题: 求解惑
public static void main(String[] args) {

                String s1="abc";
                String s2="abcd";
                String s3="abcdfd";
                String s4="abc";
                String s5="abwwcd";
                String s6="abcder";
                String s7="abc";

                ArrayList<String> aList=new ArrayList<String>();

                aList.add(s1);
                aList.add(s2);
                aList.add(s3);
                aList.add(s4);
                aList.add(s5);
                aList.add(s6);
                aList.add(s7);

                show(aList);

        System.out.println(aList);
        }

       
        public static void show(ArrayList aList){
               
        Iterator<String> it=aList.iterator();
       
        try{       



               
                while(it.hasNext()){

                String str=it.next();

                if("abc".equals(str)){

                       
                        aList.remove(str);
                       
                        show(aList);//为什么要用递归?
                }
        }
       
        }catch(Exception e){
               
        }
}
}

问什么要用递归啊,  不是有while循环么? 循环一次 如果有abc 就删除  用迭代器遍历而不用递归该怎么改?




作者: 惠晖    时间: 2012-12-11 01:02
用iterator的方法
import java.util.*;
class Lun
{
        public static void main(String[] args)
         {
                String s1="abc";
                String s2="abcd";
                String s3="abcdfd";
                String s4="abc";
                String s5="abwwcd";
                String s6="abcder";
                String s7="abc";
                ArrayList<String> al=new ArrayList<String>();
                al.add(s1);
                al.add(s2);
                al.add(s3);
                al.add(s4);
                al.add(s5);
                al.add(s6);
                al.add(s7);
                                System.out.println("原集合"+al);
                                al=getNewElements(al);
             System.out.println(al);
        }
         public static ArrayList<String> getNewElements(ArrayList<String> al)
        {
                 ArrayList<String> al1= new ArrayList<String>();
                                 Iterator<String> it=al.iterator();
                                 while (it.hasNext())
                                 {
                                        String s=it.next();
                                        if (!(s.equals("abc")))
                                        {
                                                al1.add(s);
                                        }
                                 }
                                 return al1;
        }
}
  

        
        这题目用递归明显是代码变少了吧?
作者: 奋斗的青春    时间: 2012-12-11 01:15
本帖最后由 吴愿涛 于 2012-12-11 14:37 编辑
  1. package com.itheima.chendonglian3;

  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.ListIterator;

  6. /**
  7. * 楼主的问题我不得不说几句了 ,
  8. *首先,字符串有必要声明后添加到集合吗 ?!
  9. *再者,针对楼主的问题我提供如下的解决方案 ,各位看官请过目:
  10. * 一个ArrayList对象aList对象中存在若干个字符串元素,现欲遍历该ArrayList对象,删除其中所有值为"abc"的字符串元素
  11. *
  12. */
  13. public class ArrayListTest {
  14. public static void main(String[] args) {
  15. System.out.println("--------第一种------------");
  16. removeByFor();
  17. System.out.println("--------第二种------------");
  18. removeByIterator1();
  19. System.out.println("--------第三种-----------------------");
  20. removeByIterator2();
  21. System.out.println("--------第四种-----------------------");
  22. removeByListIterator();
  23. System.out.println("----第五种增强for循环仅限于查询-------");
  24. queryByForEach();
  25. }
  26. /*
  27. * 使用一般for直接对集合进行删除
  28. */
  29. public static void removeByFor() {
  30. List<String> aList = new ArrayList<String>();
  31. aList.add("abc");
  32. aList.add("abcd");
  33. aList.add("abcde");
  34. aList.add("abc");
  35. System.out.println("删除前:" + aList);
  36. for (int i = 0; i < aList.size(); i++) {
  37. if ("abc".equals(aList.get(i))) {
  38. aList.remove(i);
  39. }

  40. }
  41. System.out.println("删除后:" + aList);
  42. }
  43. /*
  44. * 使用迭代器的remove()方法一
  45. */
  46. public static void removeByIterator1() {
  47. List<String> aList = new ArrayList<String>();
  48. aList.add("abc");
  49. aList.add("abcd");
  50. aList.add("abcde");
  51. aList.add("abc");
  52. System.out.println("删除前:" + aList);
  53. for (Iterator<String> it = aList.iterator(); it.hasNext();) {
  54. String str = it.next();
  55. if (str.equals("abc")) {
  56. it.remove();
  57. }
  58. }
  59. System.out.println("删除后:" + aList);
  60. }
  61. /*
  62. * 使用迭代器的remove()方法二
  63. */
  64. public static void removeByIterator2() {
  65. List<String> aList = new ArrayList<String>();
  66. aList.add("abc");
  67. aList.add("abcd");
  68. aList.add("abcde");
  69. aList.add("abc");
  70. System.out.println("删除前:" + aList);
  71. Iterator<String> it = aList.iterator();
  72. while (it.hasNext()) {
  73. String str = it.next();
  74. if (str.endsWith("abc"))
  75. it.remove();
  76. }
  77. System.out.println("删除后:" + aList);
  78. }

  79. /*
  80. * 使用列表迭代器 。ListIterator接口
  81. */
  82. public static void removeByListIterator() {
  83. List<String> aList = new ArrayList<String>();
  84. aList.add("abc");
  85. aList.add("abcd");
  86. aList.add("abcde");
  87. aList.add("abc");
  88. System.out.println("删除前" + aList);
  89. ListIterator<String> li = aList.listIterator();
  90. while (li.hasNext()) {
  91. String str = li.next();
  92. if (str.equals("abc")) {
  93. li.remove();
  94. }
  95. }
  96. System.out.println("删除后" + aList);

  97. }
  98. /*
  99. * 增强for循环查询,但是不能对遍历的元素进行增、删、改操作 。
  100. */
  101. public static void queryByForEach() {
  102. List<String> aList = new ArrayList<String>();
  103. aList.add("abc");
  104. aList.add("abcd");
  105. aList.add("abcde");
  106. aList.add("abc");

  107. Object[] obj = aList.toArray();
  108. for (Object o : obj) {
  109. String str = (String) o;
  110. if ("abc".equals(str)) {
  111. System.out.println("遍历出符合条件的元素是:"+str);
  112. }
  113. }
  114. }

  115. }
复制代码

作者: 颜峰    时间: 2012-12-11 01:17
这代码真心有问题,在iterator迭代时不能修改集合中的元素,否则会抛异常,使用递归无非是在catch到异常后再继续执行,而且catch块中什么都没写,表面看起来达到效果了。
还是改成for循环吧,不要用iterator了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2