扩容,伸缩数组
class Foo{
int [] arrs =new int[10];
int[] arrs1;
public void foo(int num){
for(int i=0;i<arrs.length;i++){
if(arrs[i]==0){
arrs[i] = num;
break;
}
}
int index = 0;
for(int i=0;i<arr.length;i++){
if(arrs[i]!=0){
index++;
}
}
if(arrs.leng-index<=5){
arrs1=new int[arrs.length*2];
System.arrarycopy(arrs,0,arrs1,0,index);
arrs = arrs1;
}
if(arrs.length-index==10){
arrs1=new int[arrs.length-5];
System.arraycopy(arrs,0,arrs1,0,index);
arrs=arrs1;
}
}
public void show(){
System.out.println(Arrays.toString(arrs));
}
}
public class shenSuo{
public static void main(String[] args){
Foo foo = new Foo();
foo.foo(15);
foo.foo(2);
foo.foo(13);
foo.foo(14);
foo.foo(15);
foo.foo(135);
foo.foo(13);
foo.foo(13);
foo.foo(155);
foo.foo(13);
foo.show();
}
}