zoukankan      html  css  js  c++  java
  • 集合框架-day10

    day10-集合框架-对象数组的概述与引用

    1 集合框架的简单介绍:

    • A:集合的由来
      • 数组长度是固定,当添加的元素超过了数组的长度时需要对数组重新定义,太麻烦,java内部给我们提供了集合类,能存储任意对象,长度是可以改变的,随着元素的增加而增加,随着元素的减少而减少
    • B:数组和集合的区别
      • 区别1 :
        • 数组既可以存储基本数据类型,又可以存储引用数据类型,基本数据类型存储的是值,引用数据类型存储的是地址值
        • 集合只能存储引用数据类型(对象)集合中也可以存储基本数据类型,但是在存储的时候会自动装箱变成对象
      • 区别2:
        • 数组长度是固定的,不能自动增长
        • 集合的长度的是可变的,可以根据元素的增加而增长
    • C:数组和集合什么时候用 * 1,如果元素个数是固定的推荐用数组 * 2,如果元素个数不是固定的推荐用集合

    2 Collection集合的基本功能测试

    • A:案例演示

    • 基本功能演示

      boolean add(E e) boolean remove(Object o) void clear() boolean contains(Object o) boolean isEmpty() int size()

    /*
    boolean add(E e) 添加
    boolean addAll(E e) 添加一个对象
    boolean remove(Object o) 删除
    void clear() 清空该容器中所有对象
    boolean contains(Object o) 判断集合里是否包含指定元素
    boolean isEmpty()  //判断集合是否为空
    int size() 返回集合容器大小
    */
    
    import java.util.ArrayList;
    import java.util.Collection;
    class DemoCollection {
        public static void main(String[] args) {
            //Collection c=new Collection();不能这样写,因为Collection是接口,抽象的
            Collection c=new ArrayList();
            System.out.println(c.add(100)); //添加内容可以为任意对象
            System.out.println(c.add("50"));
            Collection c0=new ArrayList();
            System.out.println(c0.add(c));  //将c添加到c0里
            System.out.println(c.remove("a")); //c对象中并没有字符串a
            System.out.println(c.remove(100)); //将100从c对象中删除
            c.clear();   //清空
            System.out.println(c);
            System.out.println(c.isEmpty());  //判断是否为空
            
            Collection c1=new ArrayList();
            c1.add(100);
            c1.add("50");
            System.out.println(c1.contains(100));  //判断100是否包含在c1对象里
            System.out.println(c1.isEmpty());
            int i=c1.size();   //返回集合容器大小
            System.out.println(i);
        }
    }
    /*
    运行结果:
    true
    true
    true
    false
    true
    []
    true
    true
    false
    2
    */
    复制代码
      • B:注意:
    • collectionXxx.java使用了未经检查或不安全的操作. 注意:要了解详细信息,请使用 -Xlint:unchecked重新编译. java编译器认为该程序存在安全隐患 温馨提示:这不是编译失败,所以先不用理会,等学了泛型你就知道了

    3 toArray集合转换为数组

    package com.hui;
    
    public class Student {
        private String name;   //私有化成员变量,只在本类中有效
        private int age;
        public Student() {}    //空参构造函数
        public Student(String name,int age) {  //有参构造函数
            this.name=name;       //将参数赋值于局部变量
            this.age=age;
        }
        public void setName(String name) {  //设置年龄
            this.name=name;
        }
        public String getName() {     //获取年龄
            return name;
        }
        public void setAge(int age) {   //设置姓名
            this.age=age;
        }
        public int getAge() {     //获取姓名
            return age;
        }
        public String toString() {
            return "姓名:"+name+"   年龄:"+age;
        }
    }
    
    /*
    * 把集合转成数组,可以实现集合的遍历
    * toArray()
    */
    package com.kai;
    import java.util.Collection;
    import java.util.ArrayList;
    import com.hui.Student;
    class DemoToArray {
        public static void main(String[] args) {
            Collection c=new ArrayList(); //父类引用指向子类对象
            c.add(new Student("小红",20));   //添加对象
            c.add(new Student("小兰",21));
            c.add(new Student("小花",22));
            c.add(new Student("小黑",23));
            /*
            解释:
            c.add(new Student("小红",20));
            add(E e)里的E指的是Object,所以这里相当于
            Object o=new Student("小红",20)
            此处的("小红",20)已经向上转型为了Object型
            */
            Object[] arr=c.toArray();      //将集合转成数组
            for(int i=0;i<arr.length;i++) {    
                Student s=(Student)arr[i];    //arr[i]为Object类型,必须向下Student转型
                System.out.println(s.toString());  //toString()可以省略不写
            }
        }
    }
    复制代码

    4 Collection的all系列

    /*
    boolean addAll(Collection c) 添加对象
    boolean removeAll(Collection c) 删除的是两个集合的交集,只要有交集就返回true,无交集flase
    boolean containsAll(Collection c) 判断调用的集合是否包含传入的集合
    boolean retainAll(Collection c) 取交集,如果调用的集合改变就返回true,如果调用的集合不变就返回false
    */
    
    import java.util.ArrayList;
    import java.util.Collection;
    class DemoCollectionAll {
        public static void main(String[] args) {
            Collection c=new ArrayList();
            c.add("a");
            c.add("b");
            c.add("c");
            c.add("d");
            Collection c1=new ArrayList();
            c1.add("e");
            c1.add("f");
            System.out.println(c.addAll(c1)); //将对象c1添加到对象c中
            System.out.println(c.containsAll(c1)); //判断调用的集合是否包含传入的集合
            Collection c2=new ArrayList();
            c2.add("a");
            c2.add("m");
            System.out.println(c.addAll(c2)); //删除的是两个集合的交集,只要有交集就返回true,无交集flase
            
            Collection c3=new ArrayList();
            c3.add("abcd");
            Collection c4=new ArrayList();
            c4.add("ab");
            System.out.println(c3.retainAll(c4)); 
            /*
            取c3与c4的交集,为ab,然后拿着交集和c3比较,不一样返回true
            */
            Collection c5=new ArrayList();
            c5.add("abcdef");
            System.out.println(c3.retainAll(c5));
            /*
            取c3与c5的交集,为abcd,然后拿着交集和c3比较,一样返回false
            */
            
        }
    }
    /*
    运行结果:
    true
    true
    true
    true
    false
    */
    复制代码

    5 迭代器

    • 迭代器概述
      • 集合是用来存储元素,存储的元素需要查看,那么就需要迭代(遍历)
    import java.util.Collection;
    import java.util.ArrayList;
    import java.util.Iterator;
    class DemoIteration {
        public static void main(String[] args) {
            Collection c=new ArrayList();
            c.add("a");
            c.add("b");
            c.add("c");
            c.add("d");
            Iterator it=c.iterator();   //获取迭代器;
            boolean b=it.hasNext();         //hasNext返回值类型为boolean型,功能是判断集合中是否有元素,有元素就返回ture
            //也可以说成如果还有元素可以迭代,就返回ture
            Object o=it.next();        //返回迭代的下一个元素
            System.out.println(o);     //输出一个迭代的a
            
            while(it.hasNext()) {
                Object oo=it.next();   //next()有指针的功能,能接着上次的迭代结果继续迭代
                System.out.println(oo);
            }
        }
    }
    /*
    运行结果:
    a
    b
    c
    d
    */
    复制代码

    6 自定义对象的迭代

    		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());
    复制代码

    7 List集合的功能概述

    /*
    * List集合的特有功能概述
      * void add(int index,E element) //向集合的指定索引位置添加对象,其它对象的索引位置相对后移一位
      * E remove(int index)  //清除集合中指定索引位置对象
      * E get(int index)     //用来获得指定位置索引的对象
      * E set(int index,E element)  //将集合中指定索引位置的对象修改为指定的对象
    */
    import java.util.Collection;
    import java.util.ArrayList;
    import java.util.List;
    
    class DemoList {
        public static void main(String[] args) {
            List l=new ArrayList();
            l.add("a");
            l.add("b");
            l.add("hello");
            l.add("c");
            l.add("d");
            l.add(1,"m");   //添加一个对象
            System.out.println(l);
            //[a, m, b, hello, c, d]
            List ll=new ArrayList();
            ll.add("123");
            l.add(3,ll);   //添加一个List对象
            System.out.println(l);
            //[a, m, b, [123], hello, c, d]
            l.remove(3);
            System.out.println(l);  //删除索引3处的位置
            //[a, m, b, hello, c, d]
            l.get(3);    //只是获取,不改变l对象的内容
            System.out.println(l);  //l对象内容并没有改变
            //[a, m, b, hello, c, d]
            System.out.println(l.get(3));  //打印出获取的对象
            //hello
            l.set(3,"world");  //在此处已经将l修改
            System.out.println(l);
            //[a, m, b, world, c, d]
            System.out.println(l.set(3,"world")); 
            //无法理解此处出现的会是world ????????????????  
        }
    }
    /*
    运行结果;
    [a, m, b, hello, c, d]
    [a, m, b, [123], hello, c, d]
    [a, m, b, hello, c, d]
    [a, m, b, hello, c, d]
    hello
    [a, m, b, world, c, d]
    world
    */
    复制代码

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

    需求:我有一个集合,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现。

    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并发修改异常
    	}
    }*/
    复制代码
    • ConcurrentModificationException出现

    • 迭代器遍历,集合修改集合

    • 解决方案 *a:迭代器迭代元素,迭代器修改元素(ListIterator的特有功能add) *b:集合遍历元素,集合修改元素

      ListIterator lit = list.listIterator(); //如果想在遍历的过程中添加元素,可以用ListIterator中的add方法 while(lit.hasNext()) { String str = (String)lit.next(); if(str.equals("world")) { lit.add("javaee"); //list.add("javaee"); } }

    转载于:https://juejin.im/post/5bd9a23d5188257f8a79feda

  • 相关阅读:
    Java实现 计蒜客 拯救行动
    Java实现 计蒜客 拯救行动
    Java实现 LeetCode 174 地下城游戏
    Java实现 LeetCode 174 地下城游戏
    Java实现 LeetCode 174 地下城游戏
    Java实现 LeetCode 173 二叉搜索树迭代器
    Java实现 LeetCode 173 二叉搜索树迭代器
    Visual Studio的SDK配置
    怎样使用CMenu类
    mfc menu用法一
  • 原文地址:https://www.cnblogs.com/twodog/p/12135724.html
Copyright © 2011-2022 走看看