I need some function of the JAVA SDK to empty an array of objects, one way I implemented is to go through a for the array and put all its values to NULL, but if they know of some predefined function that does this work I would be grateful to the Community and especially the one who gave me the answer. regards
Asked
Active
Viewed 87 times
0
-
3Why do you need to do this? (Hint: you almost certainly don't.) – Dave Newton Jun 07 '17 at 14:43
-
Can you give more background about why you think you need to empty the array? There is a concept called "soft delete." It basically means that you could actually not empty the array but treat certain buckets as effectively empty. This might make better sense for your situation. – Tim Biegeleisen Jun 07 '17 at 14:44
-
You need to understand that *anything* you can dream of asking as newbie has been asked before. And answered. It takes less time to find zillions of answers than it took me to write down this comment ... – GhostCat Jun 07 '17 at 14:44
-
why not to create new array? – matoni Jun 07 '17 at 14:44
-
And another comment: an array might not even be the best data structure here, but with no information, we can't say for certain. – Tim Biegeleisen Jun 07 '17 at 14:45
2 Answers
0
You may use Arrays.fill(myArray, null)
assuming myArray
is your array you like to set all values to null
.

Harmlezz
- 7,972
- 27
- 35
0
You can use Arrays.fill(yourArray, null);
(Or)
There is another option, you can simply assign null
to the reference
The first option is by far the best to depend on, as the second one can break your code if there are any further references to this object until and unless it gets garbage collected. The garbage collector will clean up any references left by the array.

N00b Pr0grammer
- 4,503
- 5
- 32
- 46