清空的Java /处理数组 [英] Empty an array in Java / processing

查看:184
本文介绍了清空的Java /处理数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了通过每个数组中的元素循环,每个人设置为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 code执行此人,但它的的库函数,如果也许这就是你的意思。

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.

这当然不会让你的调整的数组(零),如果这就是你通过什么意思
空。数组大小是固定的,所以如果你想在新阵列具有不同的尺寸你最好还是重新分配引用到一个新的数组作为其他的答案展现。更好的是,使用列表键入像的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆