/** * array<>盛放的是E类型 * @param <E> */ public class Array<E> { private E[] array; private int size; /** * 构造函数初始化数组 * @param capipity */ public Array( int capacity){ array = (E[])new Object[capacity]; size = 0; } /** * 默认构造函数数组容量为10 */ public Array(){ this(10); } /** * 获取数组的总容量 * @return */ public int getCapatity(){ return array.length; } //判断数组是否为空 public boolean isEmpty(){ return array.length ==0; } //获取数组元素的个数 public int getSize(){ return size; } /** * 像数组所有元素后添加一个新元素 * @param e */ public void addLast(E e){ add(size,e); } /** * 向数组中第一个元素添加数据 * @param e */ public void addFirst(E e){ add(0,e); } //像数组中任意一个位置添加元素 public void add(int index,E e){ if(index<0 || index> size){ throw new IllegalArgumentException("索引异常,不合法的插入"); } if (size==array.length){ resize(2* array.length); } for (int i = size -1; i >=index;i--){ if (i>=index){ array[i+1] = array[i]; } } array[index] = e; size++; } //获取index位置上的元素 public E get(int index){ if(index <0 || index>=size){ throw new IllegalArgumentException("索引异常"); } return array[index]; } public void set(int index,E e){ if(index <0 || index>=size){ throw new IllegalArgumentException("索引异常"); } array[index] =e; } //判断数组是否包含元素e public boolean Cotain(E e){ for(int i = 0;i<size ;i++){ if(array[i].equals(e)){ return true; } } return false; } //查找数组元素e的索引,如果存在元素e则返回索引,否则返回-1 public int find(E e){ for(int i = 0;i<size;i++){ if(array[i]==e){ return i; } }//当循环结束的时候,如果没有返回-1 return -1; } //从数组中删除索引为index的元素,并將元素返回出來 public E remove(int index){ if(index<0 || index>=size){ throw new IllegalArgumentException("索引异常"); } E e = array[index]; for(int i=index+1;i<size;i++){ array[i-1] = array[i]; } size--; array[size]=null; if(size==array.length/2){ resize(array.length/2); } return e; } //刪除第一個元素 public E removefirst(){ return remove(0); } //删除最后一个元素 public E removelast(){ return remove(size-1); } public E deleteElete(E e){ int index = find(e); return remove(index); } @Override public String toString(){ StringBuilder res = new StringBuilder(); res.append(String.format("Array: size = %d capatity =%d\n", size,array.length)); res.append("["); for (int i = 0; i<size;i++){ res.append(array[i]); if (i!=size-1){ res.append(","); } } res.append("]"); return res.toString(); } public void resize(int capacity){ E[] newDate = (E[])new Object[capacity]; for(int i=0;i<size;i++){ newDate[i] = array[i]; } array = newDate; } }
测试:
public class Student { private int age; private String name; public Student(String name,int age){ this.name=name; this.age = age; } @Override public String toString (){ return "姓名" + this.name + "年龄" +this.age; } public static void main(String [] args){ Array<Student> arr = new Array<Student>(); arr.addLast(new Student("張三",21)); arr.addLast(new Student("李四",22)); System.out.println(arr); } }
时间复杂度分析:
时间复杂度
时间与输入数据的规模(输入数据的个数)
渐进时间复杂度:当N趋近于无穷的时候,
操作 时间复杂度
addLast(e) | O(1) |
addFirst(e) | O(n) |
add(index,e) | O(n) |
removeLast() | O(1) |
removeFirst() | O(n) |
Remove(index,e) | O(n) |
Set(index,e) | O(1) |
Get(index) | O(1) |
Find(e) | O(n) |
Contains(e) | O(n) |
添加操作 最坏情况O(n) 添加最后一个元素要resize() 也是O(n)
分析resize()时间复杂度
均摊复杂度:
当capacity=8的时候,addLast()操作
1 1 1 1 1 1 1 1 8+1第九次resize()进行9次基本操作
总共17次基本操作
平均下来,每次addLast()操作执行两次基本操作
假设 Capacity =n 时,当进行第n+1 次addLast()操作时,出发resize()操作。总共进行了2n+1次基本操作。平均下来,每次addLast()操作,进行两次基本操作。