java数组表方法的实现.docx
《java数组表方法的实现.docx》由会员分享,可在线阅读,更多相关《java数组表方法的实现.docx(8页珍藏版)》请在冰豆网上搜索。
java数组表方法的实现
Java中数组表类的各个方法:
publicclassMyArrayList{
privateinttheSize;
privateObject[]theItems;
publicMyArrayList(){
clear();
}
publicvoidclear(){
theSize=0;
ensureCapacity(10);
}
publicvoidensureCapacity(intnewCapacity){
if(newCapacityreturn;
}
Object[]old=theItems;
theItems=(Object[])newObject[newCapacity];
for(inti=0;itheItems[i]=old[i];
}
}
publicintsize(){
returntheSize;
}
publicbooleanisEmpty(){
if(size()==0)
System.out.println("Thelistisempty");
else
System.out.println("Thelistisnotempty");
returnsize()==0;
}
publicObjectget(intidx){
if(idx<0||idx>size()){
thrownewArrayIndexOutOfBoundsException();
}
returntheItems[idx];
}
publicObjectset(intidx,Objectnewvalue){
if(idx<0||idx>size()){
thrownewArrayIndexOutOfBoundsException();
}
Objectold=theItems[idx];
theItems[idx]=newvalue;
returnold;
}
publicbooleanadd(Objectx){
add(size(),x);
returntrue;
}
publicvoidadd(intidx,Objectx){
if(theItems.length==size()){
ensureCapacity(size()*2+1);
}
for(inti=theSize;i>idx;i--){
theItems[i]=theItems[i-1];
}
theItems[idx]=x;
theSize++;
}
publicObjectaddAraay(intidx,Object[]x){
if(theItems.length==size()){
ensureCapacity(size()+x.length);
}
for(inti=idx+x.length;iObject[]old=theItems;
theItems[i]=old[i-x.length];
}
//System.arraycopy(theItems,idx,theItems,idx+x.length,x.length);
for(inti=idx;itheItems[i]=x[i-idx];
}
theSize=theSize+x.length;
returntheItems;
}
publicObjectremove(intidx){
if(idx<0||idx>size()){
thrownewArrayIndexOutOfBoundsException();
}
Objectold=theItems[idx];
for(inti=idx;itheItems[i]=theItems[i+1];
}
theSize--;
returnold;
}
publicintindexOf(Objectx){
if(x==null){
for(inti=0;iif(theItems[i]==x){
returni;
}
}
else{
for(inti=0;iif(theItems[i]==x){
returni;
}
}
return-1;
}
publicbooleancontains(Objectx){
if(indexOf(x)>=0)
System.out.println("true");
else{
System.out.println("false");
}
returnindexOf(x)>=0;
}
publicvoidshowList(){
System.out.print("List:
");
for(inti=0;iif(iSystem.out.print(theItems[i]+"-");
else
System.out.print(theItems[i]);
System.out.println();
}
publicvoidshowList(Object[]x){
System.out.print("List:
");
for(inti=0;iif(iSystem.out.print(x[i]+"-");
else
System.out.print(x[i]);
System.out.println();
}
}
publicvoidturnList(){
Object[]oldList=theItems;
theItems=(Object[])newObject[size()];
System.out.println("Theoldlistis:
");
//showList();
for(inti=0;itheItems[i]=oldList[size()-1-i];
System.out.println("Theoverturnlistis:
"+theItems[i]+"");
}
System.out.println();
}
publicvoidlistNext(intidx){
Objectold=null;
if(idx<0||idx>size())
thrownewArrayIndexOutOfBoundsException();
elseif(idxold=theItems[idx+1];
System.out.println("The"+theItems[idx]+"'snextnoteis"+old);
}
else{
System.out.println("The"+theItems[idx]+"don'thasnextnote");
}
}
publicbooleanhasNext(intidx){
if(idx<0||idx>size())
thrownewArrayIndexOutOfBoundsException();
elseif(idxSystem.out.println("true");
else
System.out.println("false");
returnidx}
}
测试程序:
publicclassMyArrayListTest{
staticObject[]intE={1,2,3,4};
publicstaticvoidmain(String[]args){
MyArrayListitems=newMyArrayList();
items.ensureCapacity(10);
//System.out.println(((MyArrayList)items).size());
//items.add(0,intE[0]);
//System.out.println(((MyArrayList)items).size());
//System.out.println(((MyArrayList)items).get(0));
items.addAraay(0,intE);
//System.out.println(((MyArrayList)items).size());
//System.out.println(((MyArrayList)items).get
(1));
//System.out.println(((MyArrayList)items).set(4,5));
//System.out.println(((MyArrayList)items).get(4));
items.remove(3);
System.out.println(((MyArrayList)items).size());
System.out.println(((MyArrayList)items).get(3));
items.contains(3);
items.contains(5);
items.showList();
items.add(7);
System.out.println(((MyArrayList)items).size());
items.add(3,10);
items.showList();
items.add(2,'a');
items.showList();
items.turnList();
items.showList();
items.listNext(5);
items.listNext(4);
items.hasNext(5);
}
}