在 Java 中清空数组/处理 [英] Empty an array in Java / processing
问题描述
除了循环遍历数组中的每个元素并将每个元素设置为 null 之外,Java/处理中是否有本机函数来简单地清空数组(或销毁它,以便能够将其重新声明为新数组)?
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)?
推荐答案
有
Arrays.fill(myArray, null);
并不是说它做的事情与您自己做的事情不同(它只是遍历每个元素并将其设置为 null).它不是原生的,因为它是执行此操作的纯 Java 代码,但如果这就是您的意思,它是一个库函数.
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 is a library function if maybe that's what you meant.
这当然不允许您调整数组的大小(为零),如果这就是您的意思空的".数组大小是固定的,因此如果您希望新"数组具有不同的维度,您最好像其他答案所演示的那样将引用重新分配给新数组.更好的是,使用 List
类型,例如 ArrayList
,它可以具有可变大小.
This of course doesn't allow you to resize the 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.
这篇关于在 Java 中清空数组/处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!