【學習筆記】刪除數組中指定索引處的元素


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    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM