封装数组
我们对数组进行二次封装,使数组能指定大小,判断是否为空,获取元素个数等功能:
public class Array<E> {
private E[]data;
private int size;
// 构造函数,传入数组的容量capacity构造Array
public Array(int capacity){
data = (E[]) new Object[capacity];
size = 0;
}
// 无参数的构造函数,默认数组的容量capacity=10
public Array(){
this(10);
}
// 获取数组的容量
public int getCapacity(){
return data.length;
}
// 获取数组中的元素个数
public int getSize(){
return size;
}
// 返回数组是否为空
public boolean isEmpty(){
return size == 0;
}
对数组元素进行增删改查
增加元素与扩容
由于我们有时不明确数组究竟需要多大容量,所以这时需要进行自动扩容,原理就是插入数据时开创一个新的容量更大的数组,并将元素复制进去,data再指向新数组。
// 将数组空间的容量变成newCapacity大小
private void resize(int newCapacity){
//开辟新数组
E[] newData = (E[])new Object[newCapacity];
//复制元素
for(int i = 0 ; i < size ; i ++)
newData[i] = data[i];
//指向新数组
data = newData;
}
// 在index索引的位置插入一个新元素e
public void add(int index, E e){
//如果索引非法,抛出异常
if(index < 0 || index > size)
throw new IllegalArgumentException
("Add failed. Require index >= 0 and index <= size.");
//如果空间已满,扩容两倍
if(size == data.length)
resize(2*data.length);
//把index之后的元素向后挪一个位置
for(int i = size - 1; i >= index ; i --)
data[i + 1] = data[i];
//插入新元素
data[index] = e;
//增加容量
size ++;
}
// 向所有元素后添加一个新元素
public void addLast(E e){
add(size, e);
}
// 在所有元素前添加一个新元素
public void addFirst(E e){
add(0, e);
}
查询和修改元素
// 获取index索引位置的元素
public E get(int index){
//索引非法,抛出异常
if(index < 0 || index >= size)
throw new IllegalArgumentException("Get failed. Index is illegal.");
return data[index];
}
// 修改index索引位置的元素为e
public void set(int index, E e){
//索引非法,抛出异常
if(index < 0 || index >= size)
throw new IllegalArgumentException("Set failed. Index is illegal.");
data[index] = e;
}
//输出数组信息
@Override
public String toString(){
StringBuilder res = new StringBuilder();
res.append(String.format
("Array: size = %d , capacity = %d\n", size, data.length));
res.append('[');
for(int i = 0 ; i < size ; i ++){
res.append(data[i]);
if(i != size - 1)
res.append(", ");
}
res.append(']');
return res.toString();
}
包含,搜索和删除元素
对于addLast和removeLast,每隔n次才会触发resize操作,所以resize的均摊复杂度为O(1)。当我们同时看addLast和removeLast操作:
如图:当容量已满时添加元素就会扩容,这时容量变成了二倍,时间复杂度为O(n)。之后马上进行删除操作,删除后元素个数为容量的一半,就会缩小容量,时间复杂度为O(n)。这样反复调用,时间复杂度不再是均摊复杂度为O(1)发生了复杂度震荡。
出现原因:removeLast时resize操作过于着急。
解决方案:当size==capacity/4时,再将capacity减半。这样就预留出了原来容量的1/4,这时再添加元素也不用马上扩容了。
// 查找数组中是否有元素e
public boolean contains(E e){
for(int i = 0 ; i < size ; i ++){
if(data[i] == e)
return true;
}
return false;
}
// 查找数组中元素e所在的索引,如果不存在元素e,则返回-1
public int find(E e){
for(int i = 0 ; i < size ; i ++){
if(data[i] == e)
return i;
}
return -1;
}
// 从数组中删除index位置的元素, 返回删除的元素
public E remove(int index){
if(index < 0 || index >= size)
throw new IllegalArgumentException("Remove failed. Index is illegal.");
//index之后的元素前移一个位置
E ret = data[index];
for(int i = index + 1 ; i < size ; i ++)
data[i - 1] = data[i];
size --;
/*如果当前元素个数只有数组容量的1/4时,缩小容量为当前的1/2,
由于缩容时容量可能为1,则data.length / 2为0,无法开辟容量为0的数组*/
if(size == data.length / 4 && data.length / 2 != 0)
resize(data.length / 2);
return ret;
}
// 从数组中删除第一个元素, 返回删除的元素
public E removeFirst(){
return remove(0);
}
// 从数组中删除最后一个元素, 返回删除的元素
public E removeLast(){
return remove(size - 1);
}
// 从数组中删除元素e
public void removeElement(E e){
int index = find(e);
if(index != -1)
remove(index);
}
第一篇文章,写得不好,大家见谅!