public class intArray {
private int theSize=0;;
private int a_int[]={};
public intArray(int a[]){
this.a_int=a;
this.theSize=a.length;
}
public intArray(){
this.theSize=0;
}
public int getSize(){
return this.theSize;
}
public int getAt(int nIndex){
return this.a_int[nIndex];
}
public void SetAt(int nIndex , int newElement){
this.a_int[nIndex] =newElement;
}
public void add(int nIndex){
int old[] = this.a_int;
this.a_int= new int[this.theSize+1];
for(int i =0;i
a_int[i]= old[i];
}
if(this.theSize>0){
a_int[this.theSize]=nIndex;
}else{
a_int[0]=nIndex;
}
this.theSize++;
}
public static void main(String args[]){
intArray array = new intArray();
array.add(1);
array.add(2);
array.add(3);
array.add(4);
array.add(5);
for(int i = 0;i
System.out.println(array.getAt(i));
}
}
}
再给个任意类型的
public class myArray {
private int theSize=0;
private AnyType theItem[]=null;
public myArray(AnyType a[]){
this.theItem=a;
this.theSize=a.length;
}
public myArray(){
}
public int getSize(){
return this.theSize;
}
public AnyType get(int nIndex){
return this.theItem[nIndex];
}
public void Set(int nIndex , AnyType newElement){
this.theItem[nIndex] =newElement;
}
public void add(AnyType newVal){
AnyType old[]=this.theItem;
this.theItem= (AnyType[]) new Object[theSize+1];
if(theSize!=0){
for(int i =0;i
this.theItem[i]= old[i];
}
}
this.theItem[this.theSize]=newVal;
this.theSize++;
}
public static void main(String args[]){
myArray array = new myArray();
array.add("1");
array.add("2");
array.add("3");
array.add("a");
array.add("b");
array.add("c");
for(int i =0;i
System.out.println(array.get(i));
}
}
}
|