动态数组实现
import java.util.Arrays;
import java.util.Objects;
/**
* @author john
* @date 2020/11/27 - 8:43
*/
@SuppressWarnings("unchecked")
public class ArrayList<E> {
// 元素的数量
private int size;
// 所有的元素
private E[] elements;
private static final int DEFAULT_CAPACITY = 10;
private static final int ELEMENT_NOT_FOUND = -1;
public ArrayList(int capacity) {
capacity = Math.max(capacity, DEFAULT_CAPACITY);
elements = (E[]) new Object[capacity];
}
public ArrayList() {
this(DEFAULT_CAPACITY);
}
/**
* 清除所有元素
*/
public void clear() {
for (int i = 0; i < size; i++) {
elements[i] = null;
}
size = 0;
}
/**
* 获取当前数组元素的数量
*
* @return
*/
public int size() {
return size;
}
/**
* 判断数组是否为空
*
* @return
*/
public boolean isEmpty() {
return size == 0;
}
/**
* 是否包含某个元素
*
* @param element
* @return
*/
public boolean contains(E element) {
return indexOf(element) != ELEMENT_NOT_FOUND;
}
/**
* 查看元素索引
*
* @param element
* @return
*/
public int indexOf(E element) {
if (element == null) {
for (int i = 0; i < size; i++) {
if (elements[i] == null) return i;
}
} else {
for (int i = 0; i < size; i++) {
if (element.equals(elements[i])) return i;
}
}
return ELEMENT_NOT_FOUND;
}
/**
* 添加元素到尾部
*
* @param element
*/
public void add(E element) {
add(size, element);
}
/**
* 获取index位置的元素
*
* @param index
* @return
*/
public E get(int index) {
rangeCheck(index);
return elements[index];
}
/**
* 设置index位置的元素
*
* @param index
* @param element
* @return
*/
public E set(int index, E element) {
rangeCheck(index);
E old = elements[index];
elements[index] = element;
return old;
}
/**
* 在index位置插入一个元素
*
* @param index
* @param element
*/
public void add(int index, E element) {
rangeCheckForAdd(index);
ensureCapacity(size + 1);
for (int i = size; i > index; i--) {
elements[i] = elements[i - 1];
}
elements[index] = element;
size++;
}
/**
* 删除指定index位置的元素
*
* @param index
* @return
*/
public E remove(int index) {
rangeCheck(index);
E old = elements[index];
for (int i = index + 1; i < size; i++) {
elements[i - 1] = elements[i];
}
elements[--size] = null;
return old;
}
/**
* 保证要有capacity的容量
*
* @param capacity
*/
private void ensureCapacity(int capacity) {
int oldCapacity = elements.length;
if (oldCapacity >= capacity) return;
// 新容量为旧容量的1.5倍
int newCapacity = oldCapacity + (oldCapacity >> 1);
E[] newElements = (E[]) new Object[newCapacity];
for (int i = 0; i < size; i++) {
newElements[i] = elements[i];
}
elements = newElements;
System.out.println(oldCapacity + "扩容为" + newCapacity);
}
// 抛索引越界异常
private void outOfBound(int index) {
throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size);
}
private void rangeCheck(int index) {
if (index < 0 || index >= size) {
outOfBound(index);
}
}
private void rangeCheckForAdd(int index) {
if (index < 0 || index > size) {
outOfBound(index);
}
}
@Override
public String toString() {
StringBuilder string = new StringBuilder();
string.append("size=").append(size).append(", [");
for (int i = 0; i < size; i++) {
if (i != 0) {
string.append(", ");
}
string.append(elements[i]);
}
string.append("]");
return string.toString();
}
}
测试类
/**
* @author john
* @date 2020/11/27 - 9:27
*/
public class Person {
private int age;
private String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Person [age=" + age + ", name=" + name + "]";
}
@Override
protected void finalize() throws Throwable {
super.finalize();
System.out.println("Person - finalize");
}
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (obj instanceof Person) {
Person person = (Person) obj;
return this.age == person.age;
}
return false;
}
}
public class Main {
public static void main(String[] args) {
// int -> Integer
// 所有的类,最终都继承java.lang.Object
// new是向堆空间申请内存
ArrayList<Person> persons = new ArrayList<>();
persons.add(new Person(10, "Jack"));
persons.add(new Person(12, "James"));
persons.add(new Person(15, "Rose"));
persons.clear();
persons.add(new Person(22, "abc"));
System.out.println(persons);
ArrayList<Integer> ints = new ArrayList<>();
ints.add(10);
ints.add(10);
ints.add(22);
ints.add(33);
System.out.println(ints);
System.out.println(ints.indexOf(33));
ints.remove(0);
System.out.println(ints);
ints.set(0,11);
System.out.println(ints);
}
}