在自己的数组中添加,修改,查询,插入数组元素,重写数组的toString方法,是否包含一个元素
使用泛型:
让我们的数据结构可以放置任何数据类型
不可以放置基本数据类型(八种) ,只能是类对象(包装类)
java语言不支持直接new一个泛型,所以在newObject后,在前面吧数组强转成(E[])
动态数组:
数组的扩容:缩容
简单的时间复杂度分析:
O(n)----大O描述的是算法的运行时间与输入数据之间的关系,算法运行时间的多少和nums中元素的个数程线性关系
渐进时间复杂度:当n趋近与无穷时时间复杂度。
对于动态数组:
增:O(n)
删:O(n)
改:已知索引O(1)未知O(n)
查:已知索引O(1)未知O(n)
均摊复杂度:
复杂度震荡:lazy代码
自己的数组类
public class Array<E> {
private E[] data;
private int size;
@SuppressWarnings("unchecked")
public Array(int capacity) {
data = (E[]) new Object [capacity];
size = 0;
}
public Array() {
this(10);
}
//元素个数
public int getSize() {
return size;
}
//数组容量
public int getCapacity() {
return data.length;
}
//是否为空
public boolean isEmpty() {
return size == 0;
}
//添加元素
public void addLast(E e) {
add(size , 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
void set(int index,E e) {
data[index] = e;
}
//查询数组中是否有e
public boolean contains(E e ) {
for(int i = 0;i<size;i++) {
if(data[i].equals(e))return true;
}
return false;
}
//查询数组元素e的索引
public int find(E e) {
for(int i = 0;i<size;i++) {
if(data[i].equals(e))return i;
}
return -1;
}
//在第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);
}
for(int i = size-1;i>=index;i--) {
data[i+1] = data[i];//元素向后一位
}
data[index] = e;
size++;
}
//向所有元素前添加一个元素
public void addFirst(E e) {
add(0,e);
}
//删除index位置元素,返回删除的元素
public E remove(int index) {
if(index<0||index>size) {
throw new IllegalArgumentException("Remove failed.index is illgal.");
}
E ret = data[index];
for(int i = index+1;i<size;i++) {
data[i-1] = data[i];
}
size--;
data[size] = null;//loitering Objects != memory leak
if(size == data.length/4&&data.length/2!=0) {//lazy减小时间复杂度 //动态减小数组的空间
resize(data.length/2);
}
return ret;
}
public E removefirst() {
return remove(0);
}
public E removeLast() {
return remove(size-1);
}
//从数组中删除元素e
public void removeElemnet(E e) {
int index = find(e);
if(index != -1)remove(index);
}
@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();
}
private void resize(int newcapacity) {
E[] newdata = (E[])new Object[newcapacity];
for(int i = 0;i<size;i++)
newdata[i] = data[i];
data = newdata;
}
}
测试:
public class Test {
public static void main(String[] args) {
Array<Integer> a = new Array(10);
for(int i = 0;i<10;i++) {
a.addLast(i);
}
System.out.println(a);
a.add(1, 100);
System.out.println(a);
//Array:size = 11,capacity = 20 扩容
//[0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a.addFirst(-1);
System.out.println(a);
for(int i = 0;i<3;i++){
a.remove(i);
}//capacity = 10;缩容
System.out.println(a);
}
}
ps:
有时间学着用一下idea,养成一个良好的写代码的习惯比如覆盖方法用@Override
今天出现了空指针异常,最后发现是在数组的容量没有开启