Results 1 to 6 of 6
  1. #1
    Stamoulohta is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Question 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.

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    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.

  3. #3
    Stamoulohta is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    but how do I de-reference something so garbage collector picks it up?

    for instance
    Java Code:
    George me = new George();
    .
    .
    .
    int[] ls = {1,2,3,4,5};
    How do i de-dereference the instance me of class George and how do i delete a value of ls so that ls.length == 4;?

  4. #4
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    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;

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by Stamoulohta View Post
    but how do I de-reference something so garbage collector picks it up?

    for instance
    Java Code:
    George me = new George();
    .
    .
    .
    int[] ls = {1,2,3,4,5};
    How do i de-dereference the instance me of class George

    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.

  6. #6
    Stamoulohta is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    Thanks for the help guys... I got it ;)

    This collections framework seems pretty useful. Cheers!

Similar Threads

  1. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 06:48 PM
  2. Replies: 2
    Last Post: 04-20-2009, 09:00 AM
  3. help to delete
    By andykots in forum Advanced Java
    Replies: 1
    Last Post: 01-29-2009, 09:29 AM
  4. Delete
    By Sarinam in forum New To Java
    Replies: 6
    Last Post: 07-23-2008, 12:09 PM
  5. Delete From .txt file
    By Sarinam in forum New To Java
    Replies: 86
    Last Post: 06-28-2008, 11:17 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •