Empty an array in Java / processing

后端 未结 8 1394
醉梦人生
醉梦人生 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:15

    Take double array as an example, if the initial input values array is not empty, the following code snippet is superior to traditional direct for-loop in time complexity:

    public static void resetValues(double[] values) {
      int len = values.length;
      if (len > 0) {
        values[0] = 0.0;
      }
      for (int i = 1; i < len; i += i) {
        System.arraycopy(values, 0, values, i, ((len - i) < i) ? (len - i) : i);
      }
    }
    

提交回复
热议问题