• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

emptying an array

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about using Arrays.fill(array[],value)?
 
David J Evans
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Ranch Hand
    Posts: 212
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    What a stench! Central nervous system shutting down. Save yourself tiny ad!
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic