列表–整型
class MyList {
Integer[ ] vals = null;
int size = 0 ;
public MyList ( ) {
vals = new Integer [ 10 ] ;
}
public MyList ( int size) {
this . vals = new Integer [ size] ;
}
public int length ( ) {
return size;
}
public Boolean isEmpty ( ) {
if ( size == 0 ) {
return Boolean. TRUE;
}
return Boolean. FALSE;
}
public void add ( int val) {
if ( vals. length == size) {
int newSize = size << 1 ;
Integer[ ] newVals = new Integer [ newSize] ;
System. arraycopy ( vals, 0 , newVals, 0 , size) ;
vals = newVals;
}
vals[ size++ ] = val;
}
public Integer delete ( int index) {
if ( index >= size) {
throw new RuntimeException ( "长度过长" ) ;
}
Integer oldValue = vals[ index] ;
int move = size - index - 1 ;
System. arraycopy ( vals, index+ 1 , vals, index, move) ;
vals[ -- size] = null;
return oldValue;
}
public Integer get ( int index) {
if ( index >= size) {
throw new RuntimeException ( "长度过长" ) ;
}
return vals[ index] ;
}
}
列表–泛型
class MyListObject < E> {
Object[ ] vals = null;
int size = 0 ;
public MyListObject ( ) {
vals = new Object [ 10 ] ;
}
public MyListObject ( int size) {
this . vals = new Object [ size] ;
}
public int length ( ) {
return size;
}
public Boolean isEmpty ( ) {
if ( size == 0 ) {
return Boolean. TRUE;
}
return Boolean. FALSE;
}
public void add ( E val) {
if ( vals. length == size) {
int newSize = size << 1 ;
Object[ ] newVals = new Object [ newSize] ;
System. arraycopy ( vals, 0 , newVals, 0 , size) ;
vals = newVals;
}
vals[ size++ ] = val;
}
public E delete ( int index) {
if ( index >= size) {
throw new RuntimeException ( "长度过长" ) ;
}
E oldValue = ( E) vals[ index] ;
int move = size - index - 1 ;
System. arraycopy ( vals, index+ 1 , vals, index, move) ;
vals[ -- size] = null;
return oldValue;
}
public E get ( int index) {
if ( index >= size) {
throw new RuntimeException ( "长度过长" ) ;
}
return ( E) vals[ index] ;
}
}
队列
class QueueList {
int capacity;
int size = 0 ;
Integer[ ] vals;
public QueueList ( ) {
capacity = 10 ;
vals = new Integer [ capacity] ;
}
public QueueList ( int capacity) {
this . capacity = capacity;
vals = new Integer [ capacity] ;
}
public void push ( Integer val) {
if ( size == capacity) {
throw new RuntimeException ( "容量已满" ) ;
}
vals[ size++ ] = val;
}
public Integer pop ( ) {
if ( size == 0 ) {
throw new RuntimeException ( "空队列" ) ;
}
int move = size - 1 ;
Integer oldValue = vals[ 0 ] ;
System. arraycopy ( vals, 1 , vals, 0 , move) ;
vals[ -- size] = null;
return oldValue;
}
}
测试用例
public class TestQueue {
@Test
public void ArrList ( ) {
MyList list = new MyList ( 5 ) ;
for ( int i = 0 ; i < 6 ; i++ ) {
list. add ( i) ;
}
list. delete ( 0 ) ;
for ( int i = 0 ; i < 5 ; i++ ) {
System. out. println ( list. get ( i) ) ;
}
}
@Test
public void ArrQueue ( ) {
QueueList queueList = new QueueList ( 5 ) ;
for ( int i = 0 ; i < 5 ; i++ ) {
queueList. push ( i) ;
}
for ( int i = 0 ; i < 5 ; i++ ) {
Integer pop = queueList. pop ( ) ;
System. out. println ( pop) ;
}
}
@Test
public void ArrObjectList ( ) {
MyListObject< String> list = new MyListObject ( 5 ) ;
for ( int i = 0 ; i < 5 ; i++ ) {
list. add ( String. valueOf ( i) ) ;
}
for ( int i = 0 ; i < 5 ; i++ ) {
String string = list. get ( i) ;
System. out. println ( string) ;
}
}
}