File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you empty an array?

 
john von
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you empty an array?
How about multiple array?
 
Rachel Swailes
Ranch Hand
Posts: 434
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 388
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12020
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:



prints "null, null".
 
john von
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I have a follow up question, but ill just create a new thread.

Thanks again!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic