【javaSE】集合篇·上

集合篇·上

    • 1.概念
    • 2.Collection父接口
        • 演示1:集合操作字符串
        • 演示2:集合操作
    • 3.List子接口
        • 演示1:字符串类型
        • 演示2:数字类型
    • 4.List实现类介绍
    • 5.ArrayList
    • 6.ArrayList源码分析
        • 几个常量
        • 几块代码分析

1.概念

存储对象的容器,实现了对对象的一些操作,类似数组的功能

集合和数组的区别:
在这里插入图片描述
集合层次体系结构:
【javaSE】集合篇·上_第1张图片


2.Collection父接口

【javaSE】集合篇·上_第2张图片

演示1:集合操作字符串

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

public class TestStudent {
    public static void main(String[] args) {
        // 创建集合
        Collection collection = new ArrayList();

        // 添加元素
        collection.add("apple");
        collection.add("orn");
        collection.add("app");
        System.out.println(collection);

        // 删除元素
        collection.remove("app");
        System.out.println(collection);

        // 清空集合
        // collection.clear();

        System.out.println("使用增强for循环遍历集合元素:");
        // 遍历集合内容,增强型for循环
        for (Object o : collection) {
            System.out.println(o);
        }
        // 遍历集合内容,使用专属迭代器
        // hasNext();有没有下一个元素
        // next();获取下一个元素
        // remove();删除当前元素
        System.out.println("使用迭代器遍历集合元素:");
        Iterator it = collection.iterator();
        while (it.hasNext()){
            String s = (String) it.next();
            System.out.println(s);
            // it.remove();   删除元素
        }

        // 判断元素是否存在
        System.out.println(collection.contains("apple"));
        // 判断集合是否为空
        System.out.println(collection.isEmpty());
    }
}
--------------------------------
输出:
[apple, orn, app]
[apple, orn]
使用增强for循环遍历集合元素:
apple
orn
使用迭代器遍历集合元素:
apple
orn
true
false

演示2:集合操作

学生类,Student.java:

public class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        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;
    }

    // 重写toString方法
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

操作类:

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

public class TestStudent {
    public static void main(String[] args) {
        // 创建集合对象
        Collection collection = new ArrayList();

        // 添加学生数据,只是添加元素的地址
        Student s1 = new Student("dahe",18);
        Student s2 = new Student("wangwei",20);
        Student s3 = new Student("zhangsan",25);
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        System.out.println(collection.toString());

        // 删除
        collection.remove(s1);
        System.out.println(collection.toString());

        // 清空元素,只是把元素从集合里删除,元素对象依然存在
        // collection.clear();

        // 遍历
        for (Object o : collection) {
            Student s = (Student) o;
            System.out.println(s.toString());
        }
        // 迭代
        Iterator it = collection.iterator();
        while (it.hasNext()){
            Student s = (Student) it.next();
            System.out.println(s.toString());
        }
    }
}
-------------------------------------
输出:
[Student{name='dahe', age=18}, Student{name='wangwei', age=20}, Student{name='zhangsan', age=25}]
[Student{name='wangwei', age=20}, Student{name='zhangsan', age=25}]
Student{name='wangwei', age=20}
Student{name='zhangsan', age=25}
Student{name='wangwei', age=20}
Student{name='zhangsan', age=25}

3.List子接口

【javaSE】集合篇·上_第3张图片

演示1:字符串类型

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class TestStudent {
    public static void main(String[] args) {
        // 创建List集合对象
        List list = new ArrayList();

        // 添加元素
        list.add("c++");
        list.add("java");
        list.add(0,"go");
        System.out.println("元素个数:" + list.size());
        System.out.println(list);

        // 删除元素
        list.remove(0);
        System.out.println(list);

        // 遍历
        System.out.println("普通for:");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        // 增强for
        System.out.println("增强for:");
        for (Object o : list) {
            System.out.println(o);
        }
        // 迭代器
        System.out.println("迭代器:");
        Iterator it = list.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
        // 列表迭代器,ListIterator可以向前或者向后遍历,支持添加,删除,修改元素
        System.out.println("列表迭代器:");
        // 从前往后遍历
        ListIterator lit = list.listIterator();
        while (lit.hasNext()){
            System.out.println(lit.nextIndex() + ":" + lit.next());
        }
        // 从后往前遍历
        while (lit.hasPrevious()){
            System.out.println(lit.previousIndex() + ":" + lit.previous());
        }

        // 判断
        System.out.println(list.contains("java"));
        System.out.println(list.isEmpty());
        
        // 获取元素位置
        System.out.println(list.indexOf("java"));
    }
}
---------------------------------------------
输出:
元素个数:3
[go, c++, java]
[c++, java]
普通for:
c++
java
增强for:
c++
java
迭代器:
c++
java
列表迭代器:
0:c++
1:java
1:java
0:c++
true
false
1

演示2:数字类型

import java.util.ArrayList;
import java.util.List;

public class TestStudent {
    public static void main(String[] args) {
        // 创建List集合对象
        List list = new ArrayList();

        // 添加元素
        list.add(10);
        list.add(20);
        list.add(0,30);
        list.add(521);
        list.add(1314);
        System.out.println(list);

        // 返回子集合
        System.out.println(list.subList(1,3));
    }
}
--------------------------------------
输出:
[30, 10, 20, 521, 1314]
[10, 20]

4.List实现类介绍

【javaSE】集合篇·上_第4张图片


5.ArrayList

基本同上的类方法

import java.util.ArrayList;

public class TestStudent {
    public static void main(String[] args) {
        // 创建集合
        ArrayList arrayList = new ArrayList<>();

        // 添加元素
        Student s1 = new Student("唐山",12);
        Student s2 = new Student("烧烤",18);
        arrayList.add(s1);
        arrayList.add(s2);
        System.out.println(arrayList.toString());

        // 删除元素
        arrayList.remove(s1);
        System.out.println(arrayList.toString());
    }
}
-------------------------------------------
输出:
[Student{name='唐山', age=12}, Student{name='烧烤', age=18}]
[Student{name='烧烤', age=18}]

6.ArrayList源码分析

几个常量

  • 默认容量大小:
private static final int DEFAULT_CAPACITY = 10;
  • 存放元素的数组
transient Object[] elementData; // non-private to simplify nested class access
  • 实际的元素个数
private int size;

几块代码分析

第一块:集合为空的时候初始化

private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
-->
public ArrayList() {
    this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
// 集合中没有添加元素的时候,容量为0

第二块:添加元素

public boolean add(E e) {
    modCount++;
    add(e, elementData, size);
    return true;
}
-->
private void add(E e, Object[] elementData, int s) {
	// 如果空间不够了,那么调用grow()进行扩容
    if (s == elementData.length)
        elementData = grow();
    // 这里是添加元素的主要逻辑
    elementData[s] = e;
    size = s + 1;
}
-->
private Object[] grow() {
    return grow(size + 1); // 继续跳转
}
-->
// 扩容原来的1.5倍
private Object[] grow(int minCapacity) {
    int oldCapacity = elementData.length;
    if (oldCapacity > 0 || elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
        int newCapacity = ArraysSupport.newLength(oldCapacity,
                minCapacity - oldCapacity, /* minimum growth */
                oldCapacity >> 1           /* preferred growth */);
        return elementData = Arrays.copyOf(elementData, newCapacity);
    } else {
        return elementData = new Object[Math.max(DEFAULT_CAPACITY, minCapacity)];
    }
}

你可能感兴趣的:(#,javaSE,java,开发语言)