publicinterfaceList<E>{publicintgetSize();publicbooleanisEmpty();publicvoidadd(int index,E e);publicvoidaddFirst(E e);publicvoidaddLast(E e);public E get(int index);public E getFirst();public E getLast();publicvoidset(int index,E e);publicbooleancontains(E e);publicintfind(E e);public E remove(int index);public E removeFirst();public E removeLast();publicvoidremoveElement(E e);publicvoidclear();}
(2)ArrsyList类实现list接口
package 顺序表;import java.util.Arrays;/*
* 简易的顺序表
* 线性表定义:零个或多个数据的有限序列。
* 切记:顺序表就是又叫动态数组
* 而满足数组的前提就是地址大小相等并且相等的同一组数据。
* 查改时间复杂度为o(1)
* 增删时间复杂度为o(n)
* 元素允许重复
* 排序随机
* 先进先出
* 允许空值
*/publicclassArrayList<E>implementsList<E>{private E[] data;privateint size;privatestaticfinalint DEFAULT_CAPACITY =10;publicArrayList(int capacity){if(capacity <0){
capacity = DEFAULT_CAPACITY;}
data =(E[])newObject[DEFAULT_CAPACITY];this.size =0;}publicArrayList(){this(DEFAULT_CAPACITY);}@Overridepublic String toString(){
StringBuilder str =newStringBuilder();if(isEmpty()){
str.append(String.format("顺序表[]:当前容量为%d/%d\n",this.size, data.length));}else{
str.append("顺序表[");for(int i =0; i < size; i++){if(i != size -1){
str.append(data[i]+",");}else{
str.append(data[i]+"]:");}}
str.append(String.format("当前容量为%d/%d",this.size, data.length));}return str.toString();}@OverridepublicintgetSize(){// TODO Auto-generated method stubreturnthis.size;}@OverridepublicbooleanisEmpty(){// TODO Auto-generated method stubreturnthis.size ==0;}@Overridepublicvoidadd(int index, E e){// TODO Auto-generated method stubif(index <0|| index > size){thrownewIllegalArgumentException("下标有误!!!");}if(size == data.length){resize(data.length *2);}for(int i = size; i > index; i--){
data[i]= data[i -1];}
data[index]= e;
size++;}privatevoidresize(int newlength){// TODO Auto-generated method stub
E[] newData =(E[])newObject[newlength];for(int i =0; i < Math.min(newData.length, data.length); i++){
newData[i]= data[i];}
data = newData;}@OverridepublicvoidaddFirst(E e){// TODO Auto-generated method stubadd(0, e);}@OverridepublicvoidaddLast(E e){// TODO Auto-generated method stubadd(size, e);}@Overridepublic E get(int index){// TODO Auto-generated method stubif(index <0|| index >= data.length){thrownewIllegalArgumentException("下标有误!");}return data[index];}@Overridepublic E getFirst(){// TODO Auto-generated method stubreturnget(0);}@Overridepublic E getLast(){// TODO Auto-generated method stubreturnget(size -1);}@Overridepublicvoidset(int index, E e){// TODO Auto-generated method stubif(index <0|| index >= size){thrownewIllegalArgumentException("下标有误!!");}
data[index]= e;}@Overridepublicbooleancontains(E e){// TODO Auto-generated method stubreturnfind(e)!=-1;}@Overridepublicintfind(E e){// TODO Auto-generated method stubfor(int i =0; i < size; i++){if(data[i]== e){return i;}}return-1;}@Overridepublic E remove(int index){// TODO Auto-generated method stubif(index <0|| index >= size){thrownewIllegalArgumentException("下标有误!!!!!");}
E datas = data[index];for(int i = index; i < size -1; i++){
data[i]= data[i +1];}
size--;if(size == data.length /4&& size >10){resize(data.length /2);}return datas;}@Overridepublic E removeFirst(){// TODO Auto-generated method stubreturnremove(0);}@Overridepublic E removeLast(){// TODO Auto-generated method stubreturnremove(size -1);}@OverridepublicvoidremoveElement(E e){// TODO Auto-generated method stubif(find(e)!=-1){remove(find(e));}}@Overridepublicvoidclear(){// TODO Auto-generated method stub
data =(E[])newObject[DEFAULT_CAPACITY];this.size =0;}publicintgetCapacity(){return data.length;}publicvoidswap(int i,int j){if(i <0|| i >= size || j <0|| j >= size){thrownewIllegalArgumentException("交换角标非法");}
E temp = data[i];
data[i]= data[j];
data[j]= temp;}}
java实现的顺序表(1)List接口public interface List<E> { public int getSize(); public boolean isEmpty(); public void add(int index,E e); public void addFirst(E e); public void addLast(E e); p...