Results 1 to 6 of 6
Thread: delete variables
- 03-23-2011, 06:13 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 26
- Rep Power
- 0
delete variables
Hello gurus ;)
I am trying to write a small program in java and my algorithm needs to erase members of an array at some point. Could you please direct me to the most efficient way to do something like this?
And I would also like a way for deleting instances(objects) that are no longer needed to free up memory.
I thank you in advance,
George.
- 03-23-2011, 06:29 PM #2
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
Java's garbage collector will automatically free the memory objects that are no longer referenced in your program.
Your question about arrays is very vague but it sounds like an ArrayList will do what you need.
- 03-23-2011, 06:40 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 26
- Rep Power
- 0
but how do I de-reference something so garbage collector picks it up?
for instanceHow do i de-dereference the instance me of class George and how do i delete a value of ls so that ls.length == 4;?Java Code:George me = new George(); . . . int[] ls = {1,2,3,4,5};
- 03-23-2011, 06:57 PM #4
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
When your variable falls out of scope or there is no reference in your code to the variable it will be destroyed automatically. If for some reason you want to explicity dereference a variable you can set it to null. This doesn't automatically destroy the object it just makes it available to the garbage collector next time it runs.
me = null;
- 03-23-2011, 07:24 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Generally you don't do anything. You declare me, you use it and, when it is no longer in scope, you forget about it. A large part of the point of automatic garbage collection is that it is automatic.
and how do i delete a value of ls so that ls.length == 4;?
You can't. An array's length is set at the time it is initialised ("int[] ls={1,2,3,4,5};") and it will never change thereafter.
One way to "work around" this fact is to use a dummy value for array entries you wish to regard as "empty". For instance if the array elements are always going to be positive, you could put -1 in the array at positions you regard as empty. Of course ls.length won't give you the number of "filled" array positions: for that you will have to count them in a for loop (or remember that value in some other way).
A better solution is to use an instance of List to hold the data. A list will grow and shrink as required when you use its add() and remove() methods. Lists are part of the collections framework.
- 03-23-2011, 08:18 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
What are Instance variables and static variables?
By sandeshforu in forum New To JavaReplies: 3Last Post: 09-09-2009, 05:48 PM -
How to delete a JLabel by using the keyboard 'delete' key?
By Suren in forum AWT / SwingReplies: 2Last Post: 04-20-2009, 08:00 AM -
help to delete
By andykots in forum Advanced JavaReplies: 1Last Post: 01-29-2009, 08:29 AM -
Delete
By Sarinam in forum New To JavaReplies: 6Last Post: 07-23-2008, 11:09 AM -
Delete From .txt file
By Sarinam in forum New To JavaReplies: 86Last Post: 06-28-2008, 10:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks