Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

elements inside arrays

 
kevin schmidt
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • 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
 
Bill Krieger
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • 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
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • 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
 
Manish Hatwalne
Ranch Hand
Posts: 2591
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vectors are definitely my preference.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you need it synchronized(Vector) go with ArrayList.
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic