集合.ArrayList

【重点】

ArrayList:

  • 数组结构实现,查询快、增删慢

  • JDK1.2版本,运行效率快、线程不安全

源码分析:DEFAULT_CAPACITY = 10;默认容量

注意:如果没有向集合中添加任何元素时,容量0,添加一个元素之后 容量10

每次扩容大小是原来的1.5倍

elementDate存放元素的数组

size实际元素个数

add();添加元素

//源码
public
boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; } private void ensureCapacityInternal(int minCapacity) { ensureExplicitCapacity(calculateCapacity(elementData, minCapacity)); } private void ensureExplicitCapacity(int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); } private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); }
package jihe;

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

/**
 * ArrayList的使用
 * 存储结构:数组,查找遍历速度快,增删慢
 */
public class Demo05 {
    public static void main(String[] args) {
        //创建集合
        ArrayList arrayList = new ArrayList<>();
        //1.添加元素
        Student s1 = new Student("小A",20);
        Student s2 = new Student("小B",22);
        Student s3 = new Student("小C",24);
        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        System.out.println("元素个数:"+arrayList.size());
        System.out.println(arrayList.toString());
        //2.删除元素
        //arrayList.remove(new Student("小A",20));
        //System.out.println("删除之后:"+arrayList.size());

        //3.遍历元素[重点]
        //3.1使用迭代器
        System.out.println("-------3.1使用迭代器--------");
        Iterator it = arrayList.iterator();
        while (it.hasNext()){
            Student s = (Student)it.next();
            System.out.println(s.toString());
        }
        //3.2使用列表迭代器
        System.out.println("-------3.2使用列表迭代器--------");
        ListIterator lit = arrayList.listIterator();
        while (lit.hasNext()){
            Student s = (Student)lit.next();
            System.out.println(s.toString());
        }
        System.out.println("-------3.2使用列表迭代器逆序--------");
        while (lit.hasPrevious()){
            Student s = (Student)lit.previous();
            System.out.println(s.toString());
        }
        //4.判断
        System.out.println(arrayList.contains(new Student("小A",20)));
        System.out.println(arrayList.isEmpty());
        //5.查找
        System.out.println(arrayList.indexOf(new Student("小A",20)));
    }
}
package jihe;

import java.util.Objects;

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(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 +
                '}';

    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

 

posted @ 2022-08-29 17:45  sususyq-  阅读(42)  评论(0)    收藏  举报