java数组表方法的实现.docx

上传人:b****8 文档编号:30077607 上传时间:2023-08-04 格式:DOCX 页数:8 大小:15.39KB
下载 相关 举报
java数组表方法的实现.docx_第1页
第1页 / 共8页
java数组表方法的实现.docx_第2页
第2页 / 共8页
java数组表方法的实现.docx_第3页
第3页 / 共8页
java数组表方法的实现.docx_第4页
第4页 / 共8页
java数组表方法的实现.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

java数组表方法的实现.docx

《java数组表方法的实现.docx》由会员分享,可在线阅读,更多相关《java数组表方法的实现.docx(8页珍藏版)》请在冰豆网上搜索。

java数组表方法的实现.docx

java数组表方法的实现

Java中数组表类的各个方法:

publicclassMyArrayList{

privateinttheSize;

privateObject[]theItems;

publicMyArrayList(){

clear();

}

publicvoidclear(){

theSize=0;

ensureCapacity(10);

}

publicvoidensureCapacity(intnewCapacity){

if(newCapacity

return;

}

Object[]old=theItems;

theItems=(Object[])newObject[newCapacity];

for(inti=0;i

theItems[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;i

Object[]old=theItems;

theItems[i]=old[i-x.length];

}

//System.arraycopy(theItems,idx,theItems,idx+x.length,x.length);

for(inti=idx;i

theItems[i]=x[i-idx];

}

theSize=theSize+x.length;

returntheItems;

}

publicObjectremove(intidx){

if(idx<0||idx>size()){

thrownewArrayIndexOutOfBoundsException();

}

Objectold=theItems[idx];

for(inti=idx;i

theItems[i]=theItems[i+1];

}

theSize--;

returnold;

}

publicintindexOf(Objectx){

if(x==null){

for(inti=0;i

if(theItems[i]==x){

returni;

}

}

else{

for(inti=0;i

if(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;i

if(i

System.out.print(theItems[i]+"-");

else

System.out.print(theItems[i]);

System.out.println();

}

publicvoidshowList(Object[]x){

System.out.print("List:

");

for(inti=0;i

if(i

System.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;i

theItems[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(idx

old=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(idx

System.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);

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 军事

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1