Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

emptying an array  RSS feed

 
David J Evans
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Just wondering if there is a simple way to empty an array in java, without explicitly delcaring like myArray[n] = null...it's not really critical, as obviously I can write a quick function to iterate through and do it, but I was just curious to know if there is a function to just destroy the whole damn thing...kinda like the unset() function in PHP.

Thanks.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about using Arrays.fill(array[],value)?
 
David J Evans
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion - but I can't understand how to set all the values to null...all I can see (as far as I understand from the method descriptions), are ways to set the values in the array to a char/boolean/int etc...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
null (a special kind of object reference) is covered by fill(Object[] a, Object val).
 
David J Evans
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, sorry to be a pain, but I just can't work out how to use this...can you give me the code example of how to fill a 10 space array with a null value for each space.

Thanks.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try writing some code to experiment with this:
  • Create a new Object array.
  • Use Arrays' fill method to fill the array with new Object instances.
  • Iterate through the array and print each element to verify that you have Objects.
  • Use Arrays' fill method to fill the same array with null references.
  • Iterate through the array and print each element to verify that you have nulls.
  • If you're having problems with this, show us your code.
     
    David McCombs
    Ranch Hand
    Posts: 212
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A simple way would be just to create another array with the same size as the old one:

    Integer[] myArray = new Integer[10];
    ...
    myArray = new Integer[myArray.length];

    Is this better then using Arrays.fill() or looping through each element, setting it to null? I am not sure, but it is easy enough to test.
     
    Layne Lund
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What kind of array are you using? If you have declared an array of ints (int[]), then you CANNOT use null as a value. The null value is only valid for object references. I think this is why you are confused. There is no way to "empty" an array of primitive values (such as int, char, etc.).

    Layne
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!