jojo
jojo

Reputation: 333

how to delete objects?

I have two classes:

Class Node {
int address
}
Class Link{
  int latency;
  int bandwidth;
  Node node1;
  Node node2;
  }
public Link [] link= new Link[Nmax];     

if I want to create a link between two nodes, it is easy, I've just to:

node1=new Node(); //and then I add parameter like address and so on
node2= new Node();//...............
link[1]= new Link();
link[1].node1=node1;
link[1].node2=node2;
link[1].latency=15; //and so on, we suppose that we have 100 nodes and 60 links

Now, during the program, sometimes we add some new nodes then we have to add links between them, I can do this with the same manner us above, my question is: what I have to do if I want to delete a node ? (links between this node and other existing nodes must be deleted too)

Upvotes: 0

Views: 125

Answers (4)

Edwin Buck
Edwin Buck

Reputation: 70909

--- Edited in response to jpm's excellent observation ---

In your case, you are doing all of the data structure management yourself, but are not storing enough infomation to undo the additions to the data structure.

You need to store enough information at create time to support the other operations on the data structure. This means that perhaps the choice of an array as your high-level exposed data structure is a bad choice, because there is no guarantee that additions will maintain the sufficient information to support the removals.

Wrap the array in an object, and write the code in the add(...) method to support the efficient removal. This probably means storing more information, which was constructed specifically for the support of removal.

--- Original post follows ---

To delete an object in Java, ensure that nothing "points" to it (has a reference to it) and then wait. Automatic garbage collection will do the deleting for you.

An example to make this clear

 link[1] = new Link();
 // now we have an extra Link in our array
 link[1] = null;
 // now garbage collection will delete the recently added Link object.

Note that if you have two or three references to the created Link object, it will not be collected until all the references are lost

 link[1] = new Link();
 // now we have an extra Link in our array
 Link current = link[1];
 // now we have two references to the newly created Link
 link[1] = null;
 // now we have one reference to the newly created Link
 current = null;
 // now the newly created Link is a candidate for garbage collection

The way this is implemented is there is a top-level Thread of the user implemented program. If that thread can reach the Object in question then it won't get garbage collected. This means that rings and meshes of Objects that are no longer reachable from the live Threads will be collected in mass.

Upvotes: 2

robertvoliva
robertvoliva

Reputation: 902

You probably want to explore using a better data structure than an array for this use case. You want to be able to inspect all Links, figure out which ones refer to the deleted Node, and remove those Links. Start looking at List/Set and see if they suit your needs, and slowly evolve into a good implementation that gives you what you need.

Upvotes: 1

Tom
Tom

Reputation: 4180

before deleting a node; loop over your links and remove any that have your node to be deleted as node1 or node2, then delete your node.

Upvotes: 1

Till Helge
Till Helge

Reputation: 9311

"Deleting" an object in Java means to remove all references that point to the object. The Garbage Collector then eventually will free the memory occupied by the object. So what you need to do, would be to set all references to a specific Node object to null.

// lets delete node1
link[1].node1 = null;
node1 = null;

// at some point the object will be deleted

Upvotes: 0

Related Questions