【学习笔记】删除数组中指定索引处的元素

package com.xvl.test;

public class test<T>{
    /*
     * 删除数组中的某个元素
     * @index:数组索引
     * @array
     */
    public  void  removeElement(int index,T[] array){
        int numMove = array.length-index-1;
        System.arraycopy(array, index+1, array, index, numMove);
        array[array.length-1] = null;
        for (int i = 0; i < array.length; i++) {
            if(null!=array[i]){
                System.out.print(array[i].toString()+"    ");
            }
        }
    }
    public static void main(String[] args) {
        Integer array[] = {1,2,3,4,5};
        new test<>().removeElement(1, array);
    }
}

输出结果:1    3    4    5    

猜你喜欢

转载自www.cnblogs.com/deptop/p/9159503.html