#对象数组#集合

来源:互联网 发布:网络写手水军做的坏事 编辑:程序博客网 时间:2024/06/05 04:01

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

数组和集合的区别
* 1:数组既可以存储基本数据类型,又可以存储引用数据类型(存储的是对象的地址)
*  集合只能存储引用数据类型(对象)(存储的是对象的地址)
* 2:数组长度是固定的,不能自动增长
* 集合的长度的是可变的,可以根据元素的增加而增长

  • 案例演示
    • 需求:我有5个学生,请把这个5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。
  • 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]);}

15.02_集合框架(对象数组的内存图解)

  • A:画图演示
    • 把学生数组的案例画图讲解
    • 数组和集合存储引用数据类型,存的都是地址值

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

  • A:集合的由来
    • 数组长度是固定,当需要增加和减少元素时需要对数组重新定义,太麻烦,java内部给我们提供了集合类,能存储任意对象,长度是可以改变的,随着元素的增加而增加,随着元素的减少而减少。
    • 集合的底层也是用数组实现的。
  • B:数组和集合的区别
    • 区别1 :
      • 数组既可以存储基本数据类型,又可以存储引用数据类型
      • 集合只能存储引用数据类型(对象)
    • 区别2:
      • 数组长度是固定的,不能自动增长
      • 集合的长度的是可变的,可以根据元素的增加而增长
  • 数组和集合什么时候用:
  •  * 1,如果元素个数是固定的推荐用数组 * 2,如果元素个数不是固定的推荐用集合
  • C:集合继承体系图

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

  • A:案例演示
  • 基本功能演示boolean add(E e)//添加void clear()//清空集合boolean contains(Object o)//判断是否包含传入的元素,包含返回true
  • /*boolean add(E e) * 确保此 collection 包含指定的元素(可选操作)。 * collection对象添加e,成功返回true,不成功flase */Collection coll = new ArrayList();//父类引用指向子类对象boolean b1 = coll.add(1);//new Integer(1);自动装箱boolean b4 = coll.add(1);//new Integer(1);自动装箱boolean b2 = coll.add("abc");coll.add("e");System.out.println(b1);System.out.println(b2);System.out.println(b4);System.out.println(coll);//ArrayList的间接父类重写了toString方法boolean a1 = coll.contains("a");//判断是否包含传入的元素,包含返回trueboolean a2 = coll.contains("e");System.out.println(a1);System.out.println(a2);coll.clear();//清空集合System.out.println(coll);***************************************truetruetrue[1, 1, abc, e]falsetrue[]
  • boolean equals()//集合和集合之间的比较,元素一样,顺序一致返回true                                                            
    Collection coll1 = new ArrayList();coll1.add("a");coll1.add("b");coll1.add("c");coll1.add("d");//boolean b1 = coll.equals("abcd");//equals不是集合和元素之间的比较.所以这种写法是错的。Collection coll2 = new ArrayList();coll2.add("a");coll2.add("b");coll2.add("c");boolean b2 = coll1.equals(coll2);//集合和集合之间的比较,元素一样,顺序一致返回trueSystem.out.println(b2);**********************************flase
  • boolean isEmpty()//判断集合是否为空,为空返回true                                                                                    
    Collection coll = new ArrayList();boolean b1 = coll.isEmpty();//判断集合是否为空,为空返回trueSystem.out.println(b1);coll.add("a");boolean b2 = coll.isEmpty();System.out.println(b2);******************************truefalse
  • int size()//返回集合中元素个数
    Collection coll = new ArrayList();coll.add("a");coll.add("b");coll.add("c");coll.add("d");System.out.println(coll.size()); //返回集合中元素个数*************************************4

  • boolean remove()//删除成功返回true                                                                                              
    Collection coll = new ArrayList();coll.add("a");coll.add("b");coll.add("c");coll.add("d");boolean b1 = coll.remove("b");//删除成功返回trueboolean b2 = coll.remove("z");System.out.println(b1);System.out.println(b2);*************************************truefalse

  • Object[] toArray()//集合转化成数组                                                                                                
    Collection coll = new ArrayList();coll.add("a");coll.add("b");coll.add("c");coll.add("d");Object[] arr = coll.toArray();//集合转化成数组for (int i = 0; i < arr.length; i++) {System.out.print(arr[i]+" ");}*************************************a b c d


  • add()方法与addAll()方法的区别

  • Collection coll1 = new ArrayList();coll1.add("a");coll1.add("b");coll1.add("c");coll1.add("d");Collection coll2 = new ArrayList();coll2.add("e");coll2.add("f");coll2.add("g");coll2.add("h");/*coll1.addAll(coll2);//将集合对象中的每个对象遍历添加到调用集合对象中 * 集合coll1[a, b, c, d, e, f, g, h],集合元素个数:8 */coll1.add(coll2);//将整个集合对象当作一个对象,添加到调用的集合中/*集合coll1[a, b, c, d, [e, f, g, h]],集合元素个数:5*/

  • boolean containsAll(Collection coll)//判断调用的集合是否包含传入的集合coll

  • boolean removeAll(Collection coll)//删除的是交集,两个集合有交集返回true,并删除交集部分

  • Collection coll1 = new ArrayList();coll1.add("a");coll1.add("b");coll1.add("c");coll1.add("d");Collection coll2 = new ArrayList();coll2.add("a");coll2.add("b");coll2.add("z");boolean b1 = coll1.removeAll(coll2);//删除的是交集,两个集合有交集返回true,并删除交集部分System.out.println(b1);System.out.println(coll1);*************************************true[c,d]

    boolean retainAll(Collection coll2);//取交集,看调用的集合是否改变,如果没有改变返回false,如果改变返回true

  • Collection coll1 = new ArrayList();coll1.add("a");coll1.add("b");coll1.add("c");coll1.add("d");Collection coll2 = new ArrayList();coll2.add("x");coll2.add("y");coll2.add("z");boolean b1 = coll1.retainAll(coll2);//coll1与coll2的交集是空集,所以coll1称为空集System.out.println(b1);System.out.println(coll1);*********************************true[]

  • B:注意:

  • collectionXxx.java使用了未经检查或不安全的操作.注意:要了解详细信息,请使用 -Xlint:unchecked重新编译.java编译器认为该程序存在安全隐患温馨提示:这不是编译失败,所以先不用理会,等学了泛型你就知道了

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

  • A:集合的遍历
    • 其实就是依次获取集合中的每一个元素。
  • B:案例演示

    • 把集合转成数组,可以实现集合的遍历
    • toArray() *

      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());}

15.06_集合框架(Collection集合的高级功能测试)

  • A:案例演示
  • 带All的功能演示boolean addAll(Collection c)boolean removeAll(Collection c)boolean containsAll(Collection c)boolean retainAll(Collection c)

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

  • A:迭代器概述
    • 集合是用来存储元素,存储的元素需要查看,那么就需要迭代(遍历)
    • Iterator:对Collection进行迭代的迭代器
  • B:案例演示

    • 迭代器的使用

      Collection c = new ArrayList();c.add("a");c.add("b");c.add("c");c.add("d");Iterator it = c.iterator();  //获取迭代器的引用while(it.hasNext()) {      //集合中的迭代方法(遍历)    System.out.println(it.next());}
  • Iterator迭代器的方法:
  • boolean hasNext()//如果仍有元素可以迭代,则返回true。(换句话说,如果next 返回了元素而不是抛出异常,则返回true)。 
  • E next()// 返回迭代的下一个元素。若没有迭代元素,会抛出异常:NoSuchElementException找不到元素异常
  • Collection c = new ArrayList();c.add("a");c.add("b");c.add("c");c.add("d");//利用迭代器遍历集合Iterator it = c.iterator();//获取迭代器的引用while(it.hasNext()) {//没有元素可迭代就退出循环//集合中迭代方法(遍历)System.out.println(it.next());//打印下次迭代的元素}******************************************abcd
  • void remove()//从迭代器指向的 collection 中移除迭代器返回的最后一个元素(可选操作)。

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

  • A:案例演示

    • 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()); //获取对象中的姓名和年龄}

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

  • A:迭代器原理
    • 迭代器原理:迭代器是对集合进行遍历,而每一个集合内部的存储结构都是不同的,所以每一个集合存和取都是不一样,那么就需要在每一个类中定义hasNext()和next()方法,这样做是可以的,但是会让整个集合体系过于臃肿,迭代器是将这样的方法向上抽取出接口,然后在每个类的内部,定义自己迭代方式,这样做的好处有二,第一规定了整个集合体系的遍历方式都是hasNext()和next()方法,第二,代码有底层内部实现,使用者不用管怎么实现的,会用即可
  • B:迭代器源码解析
    • 1,在eclipse中ctrl + shift + t找到ArrayList类
    • 2,ctrl+o查找iterator()方法
    • 3,查看返回值类型是new Itr(),说明Itr这个类实现Iterator接口
    • 4,查找Itr这个内部类,发现重写了Iterator中的所有抽象方法

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

  • A:List集合的特有功能概述
    • void add(int index,E element)//在指定位置添加元素
    • viod addAll(int index,List list )//在指定位置向调用的集合中添加集合
    • E remove(int index)//移除列表中指定位置的元素(可选操作)。
    • List list = new ArrayList();list.add("a");list.add("b");list.add("b");list.add("b");list.add("c");list.add("d");Object obj = list.remove(1);//根据索引删除,返回被删除的对象System.out.println(obj);System.out.println(list);boolean b1 = list.remove("b");//根据对象删除System.out.println(b1);System.out.println(list);****************************************b[a, b, b, c, d]true[a, b, c, d]

    • E get(int index)//通过索引获取集合中的元素,如果索引不存在会抛出IndexOutOfBoundsException
    • int indexOf(Object o)//通过对象获取索引,返回此列表中第一次出现的指定元素的索引;如果此列表不包含该元素,则返回 -1。
    • int lastIndexOf(Object o)//从后向前遍历,获取对象的索引
    • List list = new ArrayList();list.add("a");list.add("b");list.add("b");list.add("c");list.add("d");Object obj = list.get(1);//通过索引获取集合中的元素,如果索引不存在会抛出IndexOutOfBoundsExceptionSystem.out.println(obj);int index = list.indexOf("b");//通过对象获取索引System.out.println(index);int index2 = list.lastIndexOf("b");//从后向前遍历,获取对象的索引System.out.println(index2);**************************************b12
    • E set(int index,E element)//用指定元素替换列表中指定位置的元素(可选操作)。
    • List subList(int index,int index);//截取子集合,包含头,不包含尾
    • List list = new ArrayList();list.add("a");list.add("b");list.add("c");list.add("d");list.add("e");list.add("f");list.set(1, "z");//在指定位置修改System.out.println(list);List newList = list.subList(2, 4);//截取子集合,包含头,不包含尾System.out.println(list);System.out.println(newList);*****************************************[a, z, c, d, e, f][a, z, c, d, e, f][c, d]


15.11_集合框架(List集合的特有遍历功能)

  • A:案例演示

    • 通过size()和get()方法结合使用遍历。

      List list = new ArrayList();list.add("a");list.add("b");list.add("c");list.add("d");list.add("e");//这种遍历只支持list集合,set集合不可以,因为set集合无索引for(int i = 0; i < list.size(); i++) {              System.out.println(list.get(i));                //根据索引获取值}

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

  • A:案例演示

    • List集合存储学生对象并遍历。
    • 通过size()和get()方法结合使用遍历。

      List list = new ArrayList();list.add(new Student("马哥", 18));list.add(new Student("马粉1", 18));list.add(new Student("马粉2", 18));list.add(new Student("马粉3", 18));list.add(new Student("马粉4", 18));for(int i = 0; i < list.size(); i++) {    Student s = (Student)list.get(i);    System.out.println(s.getName() + "," + s.getAge());}

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

  • A:案例演示

    • 需求:我有一个集合,请问,我想判断里面有没有"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并发修改异常    }}*/
  • B:ConcurrentModificationException出现原因:

    • 迭代器遍历集合过程中修改集合
  • C:解决方案

    • 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");//会有异常产生    }}

15.14_集合框架(ListIterator系列表迭代器)

系列表迭代器,允许程序员按任一方向遍历列表、迭代期间修改列表,并获得迭代器在列表中的当前位置。ListIterator 没有当前元素;它的光标位置 始终位于调用previous() 所返回的元素和调用next() 所返回的元素之间。
  • boolean hasNext()是否有下一个
  • boolean hasPrevious()是否有前一个
  • Object next()返回下一个元素
  • Object previous();返回上一个元素

15.15_集合框架(Vector的特有功能)

  • A:Vector类概述
  • Vector 类可以实现可增长的对象数组。与数组一样,它包含可以使用整数索引进行访问的组件。但是,Vector 的大小可以根据需要增大或缩小,以适应创建 Vector 后进行添加或移除项的操作。 
  • B:Vector类特有功能
    • public void addElement(E obj)//将指定的组件添加到此向量的末尾,将其大小增加 1。
    • public E elementAt(int index)// 返回指定索引处的组件。
    • public Enumeration elements()//  返回此向量的组件的枚举。
  • C:案例演示

    • 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());//获取集合中的元素}

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

  • A:数组
    • 查询快修改也快
    • 增删慢
  • B:链表
    • 查询慢,修改也慢
    • 增删快

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

  • A:List的三个子类的特点
  • ArrayList:    底层数据结构是数组,查询快,增删慢。    线程不安全,效率高。Vector:    底层数据结构是数组,查询快,增删慢。    线程安全,效率低。Vector相对ArrayList查询慢(线程安全的)Vector相对LinkedList增删慢(数组结构)LinkedList:    底层数据结构是链表,查询慢,增删快。    线程不安全,效率高。Vector和ArrayList的区别    Vector是线程安全的,效率低    ArrayList是线程不安全的,效率高ArrayList和LinkedList的区别    ArrayList底层是数组结果,查询和修改快    LinkedList底层是链表结构的,增和删比较快,查询和修改比较慢
  • B:List有三个儿子,我们到底使用谁呢? 查询多用ArrayList 增删多用LinkedList 如果都多ArrayList

15.18_day15总结

把今天的知识点总结一遍。

0 0
原创粉丝点击