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
Joined: Sep 27, 2001
Posts: 53
posted
0
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
Joined: May 08, 2001
Posts: 1932
posted
0
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
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
Joined: Sep 23, 2001
Posts: 158
posted
0
Vectors are definitely my preference.
Michael J Bruesch<br /><i>I code, therefore I am.</i>
Paul Stevens
Ranch Hand
Joined: May 17, 2001
Posts: 2823
posted
0
Unless you need it synchronized(Vector) go with ArrayList.
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
10
posted
0
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.
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.