• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

elements inside arrays

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If you are done with an element inside an array, how do you remove that element from the array completely, white space and all? When you remove an element and the white space, the array size should decrease. Now if you do a array.length(), will it recognize the new size? TIA.
kevin
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kevin schmidt:
Hi,
If you are done with an element inside an array, how do you remove that element from the array completely, white space and all? When you remove an element and the white space, the array size should decrease. Now if you do a array.length(), will it recognize the new size? TIA.
kevin


I don't know what white space has to do with arrays, I suppose it depends on what you're storing in the array. Anyway, an array is always a fixed length. You can replace one element with another, or set the value to null, but you can't just eliminate it. Also, there is no array.length() method, instaed you can refer to the array.length as a variable.
If you're dealing with a dynamically changing collection of Objects, perhaps ArrayList is a more appropriate data structure than array.
Bill
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Someone correct me if I am wrong, but since arrays (like String[] or int[] ) are fixed in length, you can not easily remove any elements from it. The only way I could imagine you doing this is using a for loop to copy all the elements of one array into another, except for the elements that you no longer want (would have to have an if statement that would pass over those elements).
If you want to be able to shrink and add to your arrays, use some sort of Collection, like an ArrayList instead, allowing you to add and remove elements when needed.
Hope this helps
Jason
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Someone correct me if I am wrong, but since arrays (like String[] or int[] ) are fixed in length, you can not easily remove any elements from it. The only way I could imagine you doing this is using a for loop to copy all the elements of one array into another, except for the elements that you no longer want (would have to have an if statement that would pass over those elements).
If you want to be able to shrink and add to your arrays, use some sort of Collection, like an ArrayList instead, allowing you to add and remove elements when needed.


For such things a Vector comes in handy.
HTH,
- Manish
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vectors are definitely my preference.
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless you need it synchronized(Vector) go with ArrayList.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, Sun is recommending that ArrayList replace Vector except in cases where you need synchronization.

Second, if you have an array of Objects, you can set those elements to null to remove them. However, this will not affect the size of the array. The size of the array is set at the creation of the array and is immutable.
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic