public class ChenqiArray {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
RisingsoftArray raOne=new RisingsoftArray();
int newLength=raOne.getArrayCount() 10;
for(int i=0;i<newLength;i ){
raOne.setItem(i,new Integer(i));
System.out.println("raOne.items[" i "]=" raOne.getItem(i));
}
newLength=raOne.getArrayCount() 5;
for(int i=0;i<newLength;i ){
raOne.setItem(i,new Integer(i));
System.out.println("raOne.items[" i "]=" raOne.getItem(i));
}
newLength=raOne.getArrayCount() 5;
raOne.setItem(newLength-1,new Integer(newLength-1));
for(int i=0;i<raOne.getArrayCount();i ){
System.out.println("raOne.items[" i "]=" raOne.getItem(i));
}
}
}
class RisingsoftArray{
private Object[] obj;
//constructor
public RisingsoftArray(int ArrayLength){
if(ArrayLength>=1){
obj=new Object[ArrayLength];
}
}
public RisingsoftArray(){
this(0);
}
public RisingsoftArray(Object[] obj){
this.obj=RisingsoftArray.cloneArray(obj);
}
//get the Array items count
public int getArrayCount(){
return (obj!=null)?obj.length:0;
}
//auto expand the Array and copy the items
public void expandArray(int n){
if(n>0 && n>getArrayCount()){
Object[] newObj=new Object[n];
for(int i=0;i<getArrayCount();newObj[i]=obj[i ]){}
obj=newObj;
}
}
//get Access
public Object getItem(int pos){
return (pos>=0 && pos<getArrayCount())?obj[pos]:null;
}
//set access
public void setItem(int pos,Object obj){
if(pos>=0 && pos<getArrayCount()){
this.obj[pos]=obj;
}
else if (pos>0||(pos==getArrayCount())) {
expandArray(pos 1);
setItem(pos,obj);
}
}
//clone a Array
public static Object[] cloneArray(Object[] obj){
if(obj.length<1){
return null;
}
else{
Object[] newObj=new Object[obj.length];
for(int i=0;i<obj.length;newObj[i]=obj[i ]){}
return newObj;
}
}
}
上一篇: 自定义SMTPAppender的源码
下一篇: 安裝Eclipse SVN Plugin
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




