1 Packagedatastructure;2 //Linear Table3 4 Public InterfaceIList {5 Public voidClear ();6 Public BooleanisEmpty ();7 Public intlength ();8 PublicObject Get (intIthrowsException;9 Public voidInsertintI,object x)throwsException;Ten Public voidRemoveintIthrowsException; One Public intindexOf (Object x); A Public voiddisplay (); - -}
1 Packagedatastructure;2Sequential table3 4 Public classSqListImplementsIList {5 Privateobject[] Listelem;6 Private intCurlen;7 PublicSqList (intmaxSize) {8Curlen =0;9Listelem =NewObject[maxsize];Ten } One Public voidClear () { ACurlen=0; - } - Public BooleanIsEmpty () { the - returnCurlen==0; - } - Public intLength () { + - returnCurlen; + } A PublicObject Get (intIthrowsException { at if(i<0| | I>curlen-1) - Throw NewException ("+i+" element does not exist "); - - returnListelem[i]; - } - in Public voidInsertintI, Object x)throwsException { - if(curlen==listelem.length) to Throw NewException ("Order table is full"); + if(i<0| | I>Curlen) - Throw NewException ("Invalid insertion position"); the for(intj=curlen;j>i;j--) *Listelem[j]=listelem[j-1]; $listelem[i]=x;Panax Notoginsengcurlen++; - } the Public voidRemoveintIthrowsException { + if(i<0| | I>curlen-1) A Throw NewException ("Delete location is illegal"); the for(intj=i;j<curlen-1;j++) +Listelem[j]=listelem[j+1]; -curlen--; $ $ } - Public intindexOf (Object x) { - intJ=0; the while(j<curlen&&!listelem[j].equals (x)) -J + +;Wuyi if(j<Curlen) the returnJ; - Else Wu return-1; - } About Public voiddisplay () { $ for(intj=0;j<curlen;j++) -System.out.print (listelem[j]+ ""); - System.out.println (); - } A +}
Java Language Establishment Order table