Java基础学习笔记15——(对象数组,集合(Collection),集合(List))

1.对象数组(掌握)

  • 数组既可以存储基本数据类型,也可以存储引用类型。它存储引用类型的数组就叫对象数组。
案例:用数组存储5个学生对象,并遍历数组
package cn.itcast_01;
/*
 * 我有5个学生,请把这5个学生的信息存储到数组中,并遍历数组,获取得到每个学生的信息
 *      学生:Student
 *      成员变量:name,age
 *      构造方法:无参,带参
 *      成员方法:getXxx()/setXxx()
 *      存储学生的数组
 * 分析:
 *      A:创建学生类
 *      B:创建学生数组(对象数组)
 *      C:创建5个学生对象,并赋值
 *      D: 把C步骤的元素,放到数组中
 *      E:遍历学生数组
 */
public class ObjectArrayDemo {
    public static void main(String[] args) {
        // 创建学生数组(对象数组)
        Student[] students = new Student[5];

        // 创建5个学生对象,并赋值
        Student s1 = new Student("林青霞",27);
        Student s2 = new Student("风清扬",30);
        Student s3 = new Student("刘意",30);
        Student s4 = new Student("赵雅芝",60);
        Student s5 = new Student("王力宏",35);

        // 把C步骤的元素,放到数组中
        students[0] = s1;
        students[1] = s2;
        students[2] = s3;
        students[3] = s4;
        students[4] = s5;

        // 遍历
        for (int x = 0; x < students.length; x++) {
            Student s = students[x]; 
            System.out.println(s.getName() + "-----" + s.getAge());

//          System.out.println(students[x].getName()+"-----"+students[x].getAge());
        }
    }
}

2.集合(Collection)(掌握)

(1)集合的由来
    我们学习的是Java -- 面向对象 -- 操作很多对象 -- 存储 -- 容器(数组和StringBuffer) -- 数组
    而数组的长度是固定,所以不适合做变化的需求,Java就提供了集合供我们使用
(2)集合与数组的区别:
    A:长度区别
        数组固定
        集合可变
    B:内容区别
        数组可以是基本类型,也可以是引用类型
        集合只能是引用类型
    C:元素内容
        数组只能存储同一种类型
        集合可以存储不同类型(其实集合一般存储的也是同一种类型)
(3)集合的继承体系结构
    由于需求不同,Java就提供了不同的集合类。这多个集合类的数据结构不同,但是它们都提供存储和遍历功能,
    我们把它们的共性不断地向上提取,最终就形成了集合的继承体系结构图。

    Collection
        |--List
            |--ArrayList
            |--Vector
            |--LinkedList
        |--Set
            |--HashSet
            |--TreeSet
(4)Collection的功能概述
    A:添加功能: boolean add(Object obj):添加一个元素
                  boolean addAll(Collection c):添加一个集合的元素

    B:删除功能: void clear():移除所有元素
                  boolean remove(Object o):移除一个元素
                  boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)

    C:判断功能: boolean contains(Object o):判断集合中是否包含指定的元素
                  boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
                  boolean isEmpty():判断集合是否为空

    D:获取功能: Iterator iterator()(重点)

    E:长度功能: int size():元素的个数
                  面试题:数组有没有length()方法呢  —— 用的是length属性。
                  字符串有没有length()方法呢 —— 有length方法     
                  集合有没有length()方法呢 —— 集合中求长度用size()方法

    F:交集功能: boolean retainAll(Collection c):两个集合都有的元素?
                  思考元素去哪了,返回的boolean又是什么意思呢?

    G:把集合转为数组(了解): Object[] toArray()
(5)Collection集合的遍历
    A:把集合转数组(了解)
    B:迭代器(集合专用方式)
(6)迭代器
    A:是集合的获取元素的方式。
    B:是依赖于集合而存在的。
    C:迭代器的原理和源码要理解
        a:为什么定义为了一个接口而不是实现类
        b:看迭代器的内部类实现
(7)Collection集合的案例(遍历方式 迭代器)
    集合的操作步骤:
        A:创建集合对象
        B:创建元素对象
        C:把元素添加到集合
        D:遍历集合
A:存储字符串并遍历
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;

public class CollectionDemo {
    public static void mian(String[] args){
        // 创建集合对象
        Collection c = new ArrayList();
        // 创建并添加元素
        c.add("hello");
        c.add("world");
        c.add("java");
        // 遍历集合
        Iterator it  = c.iterator();
        while(it.hasNext()) {
            String s = (String) it.next();
            System.out.println(s);
        }
    }
}
B:存储自定义对象并遍历
public class Student {
    private String name;
    private int age;

    public Student(){}

    public Student(String name,int age){
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName() {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge() {
        this.age = age;
    }
}

import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;

public class StudentDemo {
    public static void main(String[] args){
        // 创建集合对象
        Collection c = new ArrayList();
        // 创建学生对象
        Student s1 = new Student("林青霞",27);
        Student s2 = new Student("风清扬",30);
        Student s3 = new Student("刘意",30);
        Student s4 = new Student("武鑫",25);
        Student s5 = new Student("刘晓曲",16);
        // 添加元素
        c.add(s1);
        c.add(s2);
        c.add(s3);
        c.add(s4);
        c.add(s5);
        // 遍历集合
        Iterator it = c.iterator();
        while(it.hasNext()) {
            String s = (Student)it.next();
            System.out.println(s.getName() + "---" + s.getAge());
        }
    }
}

3:集合(List)(掌握)

(1)List是Collection的接口
    特点:有序(存储顺序和取出顺序一致),可重复。
(2)List的特有功能:
    A:添加功能  void add(int index,Object element):在指定位置添加元素
    list.add(1,"android"); 

    B:删除功能 Object remove(int index):根据索引删除元素,返回被删除的元素
    System.out.println("remove:" + list.remove(1));

    C:获取功能 Object get(int index):获取指定位置的元素
    System.out.println("get:" + list.get(1));

    D:迭代器功能 ListIterator listIterator():List集合特有的迭代器
    // 创建集合对象
    List list = new ArrayList();
    // 使用迭代器遍历
    Iterator it = list.iterator();
    while(it.hasNext()) {
        Student s = (Student) it.next();
        System.out.println(s.getName() + "---" + s.getAge());
    }
    System.out.println("-------------");

    E:修改功能 Object set(int index,Object element):根据索引修改元素,返回被修饰的元素
    System.out.println("set:" + list.set(1, "javaee"));
(3)List集合的特有遍历功能
    A:由size()和get()结合。
    B:代码演示
    // 创建集合对象
    List list = new ArrayList();
    // 创建并添加元素
    list.add("hello");
    list.add("world");
    list.add("java");
    // 遍历集合
    Iterator it = list.iterator();
    while(it.hasNext()) {
        String s = (String) it.next();
        System.out.println(s);
    }
    // 使用普通for遍历
    for(int x=0; x < list.size(); x++){
        String s = (String) list.get(x);
        System.out.println(s);
    }
(4)列表迭代器的特有功能:(了解)
    可以逆向遍历,但是要先正向遍历,所以无意义,基本不使用。、
(5)并发修改异常
    A:出现的现象
        迭代器遍历集合时,集合修改集合元素
    B:原因
        迭代器是依赖于集合的,而集合的改变迭代器并不知道
    C:解决方案
        a:迭代器遍历,迭代器修改(ListIterator)
            元素添加在刚才迭代的位置
        b:集合遍历,集合修改size()和get()
            元素添加在集合的末尾
实现样例:
public class ListIteratorDemo2 {
public static void main(String[] args) {
    // 创建List集合对象
    List list = new ArrayList();
    // 添加元素
    list.add("hello");
    list.add("world");
    list.add("java");
    // 方式1:迭代器迭代元素,迭代器修改元素
    // 因为Iterator迭代器没有添加功能,所以我们使用其子接口ListIterator
    ListIterator lit = list.listIterator(); 
    while(lit.hasNext()) {
        String s = (String)lit.next();
        if ("world".equals(s)) {
            lit.add("javaee");
        }
    }
    // 方式2:集合遍历元素,集合修改元素(普通for)
    for (int x = 0; x < list.size(); x++) {
        String s = (String)list.get(x);
        if ("world".equals(s)) {
            list.add("javaee");
        }
    }
    System.out.println("list:" + list);
}
(6)常见数据结构
    A:栈 先进后出
    B:队列 先进先出
    C:数组 查询快,增删慢
    D:链表 查询慢,增删快    
(7)List的子类特点(面试题)
    ArrayList
        底层数据结构是数组,查询快,增删慢。
        线程不安全,效率高。
    Vector
        底层数据结构是数组,查询快,增删慢。
        线程不安全,效率低
    LinkedList
        底层数据结构是链表,查询慢,增删快。
        线程不安全,效率高。  
    使用谁,看需求:
    分析:
        要安全,Vector(),即使要,也不使用这个,下一篇提及
        不要安全:ArrayList或者LinkedList
                查询多:ArrayList
                增删多:LinkedList  
    如果什么都不知道,就用ArrayList。
(8)List集合的案例(遍历方式 迭代器和普通for)
A:存储字符串并遍历
package cn.itcast_01;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/*
 * 需求:List集合存储字符串并遍历
 */
public class ListDemo {
    public static void main(String[] args) {
        // 创建集合对象
        List list = new ArrayList();
        // 创建字符串并添加字符串
        list.add("hello");
        list.add("World");
        list.add("java");
        // 遍历集合
        Iterator it = list.iterator();
        while(it.hasNext()) {
            String s = (String) it.next();
            System.out.println(s);
        }
    }
}   
B:存储自定义对象并遍历
// Student类
package cn.itcast_02;
public class Student {
    private String name;
    private int age;
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
}

// 实现
package cn.itcast_02;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/*
 * 存储自定义对象并遍历
 */
public class ListDemo {
    public static void main(String[] args) {
        // 创建集合对象
        List list = new ArrayList();
        // 创建学生对象
        Student s1 = new Student("白骨精",30);
        Student s2 = new Student("蜘蛛精",40);
        Student s3 = new Student("观音姐姐",22);
        // 把学生对象添加到集合对象中
        list.add(s1);
        list.add(s2);
        list.add(s3);   
        // 遍历
        Iterator it = list.iterator();
        while(it.hasNext()) {
            Student s = (Student) it.next();  // 向下转型
            System.out.println(s.getName() + "---" + s.getAge());
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值