How do you empty an array? How about multiple array?
Rachel Swailes
Ranch Hand
Joined: May 18, 2004
Posts: 434
posted
0
You can empty an array by running through it with a for loop and by assigning each place in the array to null or whatever your definitaion of empty is.
If it is an array of arrays then you need to run two for loops, one to loop through the outer array and one to loop through the inner arrays.
Try some code with these for loops and I'll help you out
Cheers, Rachel
karl koch
Ranch Hand
Joined: May 25, 2001
Posts: 388
posted
0
why not just create a new one ? if emptying an array is setting the elements to zero or null, then creating an new one might be faster ?? karl [ June 11, 2004: Message edited by: karl koch ]
john von
Ranch Hand
Joined: Apr 13, 2004
Posts: 49
posted
0
Is there no shortcut to it? What if i re-initialize it?
It the array "names" was not emptied through re-initialization, what will happen to the previous object "names" after it was re-intialize?
you haven't really "emptied" the original array, you've created a second, new empty array and pointed your reference to it.
the first one is still there, but you can't get to it ever again. it will eventually be garbage-collected - although you have NO control over when that is done.
Never ascribe to malice that which can be adequately explained by stupidity.
Ernest Friedman-Hill
author and iconoclast
Marshal
Note that another alternative (a better one, IMO) to the array-emptying "for" loop is to use the overloaded static "fill" methods in the java.util.Arrays class: