在 Java 中清空数组 / 处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4208655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Empty an array in Java / processing
提问by ina
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 be able to redeclare it as a new array)?
除了循环遍历数组中的每个元素并将每个元素设置为 null 之外,Java / processing 中是否有本机函数来简单地清空数组(或销毁它,以便能够将其重新声明为新数组)?
回答by jjnguy
You can simply assign null
to the reference. (This will work for any type of array, not just ints
)
您可以简单地分配null
给参考。(这适用于任何类型的数组,而不仅仅是ints
)
int[] arr = new int[]{1, 2, 3, 4};
arr = null;
This will 'clear out' the array. You can also assign a new array to that reference if you like:
这将“清除”阵列。如果您愿意,您还可以为该引用分配一个新数组:
int[] arr = new int[]{1, 2, 3, 4};
arr = new int[]{6, 7, 8, 9};
If you are worried about memory leaks, don't be. The garbage collector will clean up any references left by the array.
如果您担心内存泄漏,请不要担心。垃圾收集器将清除数组留下的任何引用。
Another example:
另一个例子:
float[] arr = ;// some array that you want to clear
arr = new float[arr.length];
This will create a new float[]
initialized to the default value for float.
这将创建一个新的float[]
初始化为 float 的默认值。
回答by Mark Peters
There's
有
Arrays.fill(myArray, null);
Not that it does anything different than you'd do on your own (it just loops through every element and sets it to null). It's not native in that it's pure Java code that performs this, but it isa library function if maybe that's what you meant.
并不是说它做的事情与您自己做的事情不同(它只是遍历每个元素并将其设置为 null)。它不是原生的,因为它是执行此操作的纯 Java 代码,但如果这就是您的意思的话,它是一个库函数。
This of course doesn't allow you to resizethe array (to zero), if that's what you meant by
"empty". Array sizes are fixed, so if you want the "new" array to have different dimensions you're best to just reassign the reference to a new array as the other answers demonstrate. Better yet, use a List
type like an ArrayList
which can have variable size.
这当然不允许您调整数组大小(为零),如果这就是您所说的“空”。数组大小是固定的,因此如果您希望“新”数组具有不同的维度,您最好像其他答案所演示的那样将引用重新分配给新数组。更好的是,使用List
像 anArrayList
这样可以具有可变大小的类型。
回答by Adam
array = new String[array.length];
回答by AlexR
I just want to add something to Mark's comment. If you want to reuse array without additional allocation, just use it again and override existing values with new ones. It will work if you fill the array sequentially. In this case just remember the last initialized element and use array until this index. It is does not matter that there is some garbage in the end of the array.
我只想在 Mark 的评论中添加一些内容。如果您想在没有额外分配的情况下重用数组,只需再次使用它并用新值覆盖现有值。如果您按顺序填充数组,它将起作用。在这种情况下,只需记住最后一个初始化元素并使用数组直到此索引。数组末尾有一些垃圾并不重要。
回答by R.Moeller
Faster clearing than Arrays.fill is with this (From Fast Serialization Lib). I just use arrayCopy (is native) to clear the array:
比 Arrays.fill 清除更快(来自快速序列化库)。我只是使用 arrayCopy(是原生的)来清除数组:
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);
}
回答by Shiqing Fan
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:
以双数组为例,如果初始输入values
数组不为空,下面的代码片段for-loop
在时间复杂度上优于传统的直接:
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);
}
}
回答by Cesar Duran
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 的数组,用于获取数据表上的所有选定项目
selected_items = null;
selected_items = [];
回答by Suraj Mahli
If Array xco
is not final then a simple reassignment would work:
如果 Arrayxco
不是最终的,那么简单的重新分配将起作用:
i.e.
IE
xco = new Float[xco .length];
This assumes you need the Array xco
to remain the same size. If that's not necessary then create an empty array:
这假设您需要 Arrayxco
保持相同的大小。如果没有必要,则创建一个空数组:
xco= new Float[0];