黑马程序员技术交流社区

标题: 怎么不输出? [打印本页]

作者: 田宇鹤    时间: 2012-12-2 13:14
标题: 怎么不输出?
package TestJiHeLei;
import java.util.ArrayList;import java.util.Iterator;//去除ArrayList中的重复元素public class TestArrayList1 {        public static void main(String[] args) {                // 创建集合 array1                ArrayList<String> array1 = new ArrayList<String>();                // 添加属性                String s1 = new String("zhangsan");                String s2 = new String("wangwu");                String s3 = new String("zhaoliu");                String s4 = new String("lisi");                String s5 = new String("lisi");                String s6 = new String("zhaoliu");                String s7 = new String("zhaoliu");
                ArrayList array2 = noRepeat(array1);                // 遍历打印 array2                Iterator it1 = array2.iterator();                while (it1.hasNext()) {                        String str = (String) it1.next();                        System.out.println(str);                }        }
        public static ArrayList noRepeat(ArrayList array1) {                // 创建一个新集合                ArrayList array2 = new ArrayList();                // 遍历旧的集合                Iterator it = array1.iterator();                while (it.hasNext()) {                        String str = (String) it.next();                        // 如果新集合里没有就添加 有就不添加                        if (!(array2.contains(str))) {                                array2.add(str);                        }                }                return array2;        }}


为什么不输出东西呢?


作者: 田宇鹤    时间: 2012-12-2 13:15
package TestJiHeLei;

import java.util.ArrayList;
import java.util.Iterator;
//去除ArrayList中的重复元素
public class TestArrayList1 {
        public static void main(String[] args) {
                // 创建集合 array1
                ArrayList<String> array1 = new ArrayList<String>();
                // 添加属性
                String s1 = new String("zhangsan");
                String s2 = new String("wangwu");
                String s3 = new String("zhaoliu");
                String s4 = new String("lisi");
                String s5 = new String("lisi");
                String s6 = new String("zhaoliu");
                String s7 = new String("zhaoliu");

                ArrayList array2 = noRepeat(array1);
                // 遍历打印 array2
                Iterator it1 = array2.iterator();
                while (it1.hasNext()) {
                        String str = (String) it1.next();
                        System.out.println(str);
                }
        }

        public static ArrayList noRepeat(ArrayList array1) {
                // 创建一个新集合
                ArrayList array2 = new ArrayList();
                // 遍历旧的集合
                Iterator it = array1.iterator();
                while (it.hasNext()) {
                        String str = (String) it.next();
                        // 如果新集合里没有就添加 有就不添加
                        if (!(array2.contains(str))) {
                                array2.add(str);
                        }
                }
                return array2;
        }
}


怎么是横着的  再发下

作者: 田宇鹤    时间: 2012-12-2 13:29
饿  找到原因了  我是拿对象的改的  有个地方没改   意外




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