Collection,List,Vector

2019-02-18  本文已影响0人  黄同学2019

day15笔记【Collection,List,Vector】

1_集合框架(对象数组的概述和使用)

        Student[] arr = new Student[5];                 //存储学生对象
    ​   arr[0] = new Student("张三", 23);
    ​   arr[1] = new Student("李四", 24);
    ​   arr[2] = new Student("王五", 25);
    ​   arr[3] = new Student("赵六", 26);
    ​   arr[4] = new Student("马哥", 20);
    ​   
    ​   for (int i = 0; i < arr.length; i++) {
    ​       System.out.println(arr[i]);
    ​   }

2_集合框架(集合的由来及集合继承体系图)

3_集合框架(Collection集合的基本功能测试)

4_集合框架(集合的遍历之集合转数组遍历)

            Collection coll = new ArrayList();
        ​   coll.add(new Student("张三",23));     //Object obj = new Student("张三",23);
        ​   coll.add(new Student("李四",24));
        ​   coll.add(new Student("王五",25));
        ​   coll.add(new Student("赵六",26));
        ​   
            Object[] arr = coll.toArray();              //将集合转换成数组
            for (int i = 0; i < arr.length; i++) {
                Student s = (Student)arr[i];            //强转成Student
                System.out.println(s.getName() + "," + s.getAge());
            }

5_集合框架(Collection集合的带All功能测试)

    ​   boolean addAll(Collection c)
    ​   boolean removeAll(Collection c)
    ​   boolean containsAll(Collection c)
    ​   boolean retainAll(Collection c)

6_集合框架(集合的遍历之迭代器遍历)

7_集合框架(Collection存储自定义对象并遍历)

            Collection c = new ArrayList();
        ​   
        ​   c.add(new Student("张三",23));
        ​   c.add(new Student("李四",24));
        ​   c.add(new Student("王五",25));
        ​   c.add(new Student("赵六",26));
        ​   c.add(new Student("赵六",26));
        ​   
            for(Iterator it = c.iterator();it.hasNext();) {
                Student s = (Student)it.next();                     //向下转型
                System.out.println(s.getName() + "," + s.getAge()); //获取对象中的姓名和年龄
            }
            System.out.println("------------------------------");
            Iterator it = c.iterator();                             //获取迭代器
            while(it.hasNext()) {                                   //判断集合中是否有元素
                //System.out.println(((Student)(it.next())).getName() + "," + ((Student)(it.next())).getAge());
                Student s = (Student)it.next();                     //向下转型
                System.out.println(s.getName() + "," + s.getAge()); //获取对象中的姓名和年龄
            }
        

8_集合框架(迭代器的原理及源码解析)(了解)

15.09_集合框架(List集合的特有功能概述和测试)

10_集合框架(List集合存储学生对象并遍历)

        List list = new ArrayList();
            list.add(new Student("张三", 18));
            list.add(new Student("李四", 18));
            list.add(new Student("王五", 18));
            list.add(new Student("赵六", 18));
  
  
        for(int i = 0; i < list.size(); i++) {
            Student s = (Student)list.get(i);
            System.out.println(s.getName() + "," + s.getAge());
        }

11_集合框架(并发修改异常产生的原因及解决方案)

     ​  List list = new ArrayList();
     ​  list.add("a");
     ​  list.add("b");
     ​  list.add("world");
     ​  list.add("d");
     ​  list.add("e");
     ​  
     ​  
        /*Iterator it = list.iterator();
        while(it.hasNext()) {
            String str = (String)it.next();
            if(str.equals("world")) {
                list.add("javaee");         //这里会抛出ConcurrentModificationException并发修改异常
            }
        }*/
            ListIterator lit = list.listIterator();     //如果想在遍历的过程中添加元素,可以用ListIterator中的add方法
        ​   while(lit.hasNext()) {
        ​       String str = (String)lit.next();
        ​       if(str.equals("world")) {
        ​           lit.add("javaee");  
        ​           //list.add("javaee");
        ​       }
        ​   }

12_集合框架(ListIterator)(了解)

13_集合框架(Vector的特有功能)(了解)


        Vector v = new Vector();                //创建集合对象,List的子类
            v.addElement("a");
            v.addElement("b");
            v.addElement("c");
            v.addElement("d");
  
  
        //Vector迭代
        Enumeration en = v.elements();          //获取枚举
        while(en.hasMoreElements()) {           //判断集合中是否有元素
            System.out.println(en.nextElement());//获取集合中的元素
        }

14_集合框架(数据结构之数组和链表)

15_集合框架(List的三个子类的特点)

上一篇 下一篇

猜你喜欢

热点阅读