Empty an array in Java / processing

后端 未结 8 1317
醉梦人生
醉梦人生 2020-11-27 19:05

Other than looping through each element in an array and setting each one to null, is there a native function in Java / processing to simply empty an array (or destroy it, to

相关标签:
8条回答
  • 2020-11-27 19:23

    I was able to do that with the following 2 lines, I had an array called selected_items used to get all selected items on a dataTable

    selected_items = null;
    selected_items = [];
    
    0 讨论(0)
  • 2020-11-27 19:32

    Faster clearing than Arrays.fill is with this (From Fast Serialization Lib). I just use arrayCopy (is native) to clear the array:

    static Object[] EmptyObjArray = new Object[10000];
    
    public static void clear(Object[] arr) {
        final int arrlen = arr.length;
        clear(arr, arrlen);
    }
    
    public static void clear(Object[] arr, int arrlen) {
        int count = 0;
        final int length = EmptyObjArray.length;
        while( arrlen - count > length) {
            System.arraycopy(EmptyObjArray,0,arr,count, length);
            count += length;
        }
        System.arraycopy(EmptyObjArray,0,arr,count, arrlen -count);
    }
    
    0 讨论(0)
提交回复
热议问题