java集合篇(七) ---- ArrayList 类
目录
七、ArrayList 类
7.1 位置
7.2 特点
7.3 构造方法
7.4 常用方法
7.5 代码举例
7.6 详解 ArrayList 的扩容机制
七、ArrayList 类
7.1 位置
ArrayList 类位于 java.util
包下
7.2 特点
- 是 List 接口的实现类
- 底层是用一个 Object 数组来存储数据
7.3 构造方法
public ArrayList() | |
作用 | 创建一个空的 ArrayList 对象,初始容量为 0 |
public ArrayList(int initialCapacity) | |
作用 | 创建一个指定初始容量的 ArrayList 对象 |
public ArrayList(Collection<? extends E> c) | |
作用 | 创建一个包含指定集合 c 的 ArrayList 对象 |
7.4 常用方法
参考 List 接口
List接口
7.5 代码举例
import java.util.ArrayList;
import java.util.Objects;class Student{private int id;private String name;public Student(int id,String name) {this.name = name;this.id = id;}@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +'}';}// 使用 ArrayList 类存储自定义类时,要重写 equals() 方法// 来确保 contains() 方法和 remove() 方法的正确执行@Overridepublic boolean equals(Object o) {if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return id == student.id && Objects.equals(name, student.name);}
}public class Test03 {public static void main(String[] args) {ArrayList arrayList = new ArrayList();Student student = new Student(1,"张三");Student student2 = new Student(2,"李四");Student student3 = new Student(3,"王五");Student student4 = new Student(4,"小明");Student student5 = new Student(5,"小红");//添加元素arrayList.add(student);arrayList.add(student2);arrayList.add(student3);arrayList.add(student4);arrayList.add(student5);//遍历元素System.out.println("-------------初始遍历-------------");for (Object object : arrayList) {System.out.println(object);}//删除元素//相当于 arrayList.remove(1);//相当于 arrayList.remove(student2); //没有重写 equals() 方法,这样是可以删除的arrayList.remove(new Student(2,"李四")); //若没有重写 equals() 方法,这样是删除不了的System.out.println("-------------删除元素后遍历-------------");for (Object object : arrayList) {System.out.println(object);}//替换元素System.out.println("-------------替换元素-------------");Student student6 = new Student(6,"梨花");Object set = arrayList.set(0, student6);System.out.println("替换前的元素为:" + set);//获取指定索引位置的元素Object object = arrayList.get(0);System.out.println("替换后的元素为:" + object);//获取集合的元素个数System.out.println("-------------集合的元素个数-------------");int size = arrayList.size();System.out.println(size);}
}
7.6 详解 ArrayList 的扩容机制
import java.util.ArrayList;
import java.util.Arrays;public class MyJiHe<E> {transient Object[] elementData;private int size;protected transient int modCount = 0;private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};private static final int DEFAULT_CAPACITY = 10;private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;public MyJiHe() {this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;}public boolean add(E e) {ensureCapacityInternal(size + 1);elementData[size++] = e;return true;}private void ensureCapacityInternal(int minCapacity) {ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));}private static int calculateCapacity(Object[] elementData, int minCapacity) {if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {return Math.max(DEFAULT_CAPACITY, minCapacity);}return minCapacity;}private void ensureExplicitCapacity(int minCapacity) {modCount++;if (minCapacity - elementData.length > 0)grow(minCapacity);}private void grow(int minCapacity) {int oldCapacity = elementData.length;int newCapacity = oldCapacity + (oldCapacity >> 1);if (newCapacity - minCapacity < 0)newCapacity = minCapacity;if (newCapacity - MAX_ARRAY_SIZE > 0)newCapacity = hugeCapacity(minCapacity);elementData = Arrays.copyOf(elementData, newCapacity);}private static int hugeCapacity(int minCapacity) {if (minCapacity < 0)throw new OutOfMemoryError();return (minCapacity > MAX_ARRAY_SIZE) ?Integer.MAX_VALUE :MAX_ARRAY_SIZE;}
}