数组集合删除算法

数组集合删除算法:

image

删除:

/**
 * 更多资料欢迎浏览凯哥学堂官网:http://kaige123.com 

 * @author 小沫
 */
public void remove(int index){
//objs的长度如果小于0或对象值小于等于0那么抛出数组越界异常
if(objs.length<0||this.index.0){
throw new IndexOutOfBoundsException();
}
if(this.index-1==index){
//当前对象的是所占长度-1等于要删除的下标,当前对象直接减减即可
this.index--;
}else{
//index为要删除的下标
//从objs的index+1开始覆盖到index
//当前对象值所占长度减去index再减1
System.arraycopy(objs,index+1,objs,index,this.index-index-1);
this.index--;
}
}

测试类:

public static void main(String[] args){
MyArrayList list=new MyArrayList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
list.add("F");

list.remove(2);
for(int i=0;list.size();i++){
System.out.println(list.get(i));
}
}

猜你喜欢

转载自l4432848.iteye.com/blog/2384233