手写简单的ArrayList
要求1: 实现扩容
要求2: 实现增删改查等功能
因为ArrayList底层是数组结构 具有下标 查询更快 直接上代码!!!
代码片段
package com.edit.util.list;
import java.util.Arrays;
import java.util.StringJoiner;
/**
* @author:Li
* @time: 2019/3/6 10:02
* @version: 1.0.0
*/
public class ArrList<E> {
/**
* 数据数组
*/
private Object[] data;
/**
* 最小空间
*/
private final int MIN_INDEX = 16;
/**
* 最大空间
*/
private final int MAX_INDEX = 0x7FFFFFFF;
/**
* 当前容量
*/
private int capacity;
/**
* 当前下标
*/
private int currentIndex = 0;
/**
* 构造方法
*
* @param capacity 给定一个空间
*/
public ArrList(int capacity) {
this.capacity = capacity;
data = new Object[capacity];
}
/**
* 无参构造 默认空间16
*/
public ArrList() {
this(16);
}
/**
* 扩容
*/
private void dilatation() {
int newCapacity = capacity;
// 是否需要扩容
if (currentIndex == capacity) {
// 扩容1.5倍
newCapacity = capacity + (capacity >> 1);
// 最小空间处理 以及 最大空间处理
if (newCapacity > MAX_INDEX) {
newCapacity = MAX_INDEX;
} else if (newCapacity < MIN_INDEX) {
newCapacity = MIN_INDEX;
}
// 扩容数据
Object[] newData = new Object[newCapacity];
// 拷贝数组
System.arraycopy(data, 0, newData, 0, currentIndex);
data = newData;
capacity = newCapacity;
}
}
/**
* 添加
* @param e 元素
*/
public void add(E e) {
dilatation();
data[currentIndex] = e;
currentIndex++;
}
/**
* 指定位置添加
* @param index 下标
* @param e 元素
*/
public void add(int index, E e) {
dilatation();
System.arraycopy(data, index, data, index + 1, currentIndex - index);
data[index] = e;
currentIndex++;
}
/**
* 获取
* @param index 下标
* @return
*/
public E get(int index) {
if (index < currentIndex) {
return (E) data[index];
} else {
throw new IndexOutOfBoundsException("index: " + index + " size:" + currentIndex);
}
}
/**
* 修改值
* @param index 下标
* @param e 新值
* @return
*/
public E set(int index, E e) {
if (index < currentIndex) {
data[index] = e;
return (E) data[index];
} else {
throw new IndexOutOfBoundsException("index: " + index + " size:" + currentIndex);
}
}
/**
* 大小
* @return
*/
public int size() {
return currentIndex;
}
/**
* 清空集合
*/
public void clear() {
data = new Object[16];
currentIndex = 0;
capacity = 16;
}
/**
* 通过下标删除元素
* @param index 下标
*/
public void remove(int index) {
if (index < currentIndex) {
System.arraycopy(data, index + 1, data, index, currentIndex - index);
currentIndex--;
} else {
throw new IndexOutOfBoundsException("index: " + index + " size:" + currentIndex);
}
}
/**
* 删除元素
* @param e 元素
* @return 是否删除成功
*/
public boolean remove(E e) {
for (int i = 0; i < currentIndex; i++) {
if (data[i].equals(e)) {
remove(i);
return true;
}
}
return false;
}
/**
* 集合是否为空
* @return
*/
public boolean isEmpty() {
return size() == 0;
}
/**
* 是否包含某个元素
* @param e 元素
* @return
*/
public boolean contains(E e) {
return indexOf(e) != -1;
}
/**
* 获得第一个匹配元素的下标
* @param e 元素
* @return 出现的下标
*/
public int indexOf(E e) {
for (int i = 0; i < currentIndex; i++) {
if (data[i].equals(e)) {
return i;
}
}
return -1;
}
/**
* 获得最后一个匹配元素的下标
* @param e 元素
* @return 出现的下标
*/
public int lastIndexOf(E e) {
for (int i = currentIndex - 1; i > -1; i--) {
if (data[i].equals(e)) {
return i;
}
}
return -1;
}
/**
* 将集合转为数组
* @return
*/
public Object[] toArray() {
return Arrays.copyOf(data, currentIndex);
}
@Override
public String toString() {
final StringJoiner join = new StringJoiner(",", "[", "]");
for (int i = 0; i < currentIndex; i++) {
join.add(String.valueOf(data[i]));
}
return join.toString();
}
}
结束
这就是手撕简单的ArrayList 感觉有用就点个赞吧 如果有错误或更好的方法评论区请多多指出 相互学习共同进步