Results 1 to 5 of 5
Thread: Java pointers? How to...
- 11-04-2008, 03:47 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 43
- Rep Power
- 0
Java pointers? How to...
I have an instance of a Node class and I have:
So as far as I know myNode is pointing to someOtherNode not making a copy.Java Code:Node myNode = someOtherNode;
I now need to set the "someOtherNode" to null but the only referance I have to it is myNode.
This should not effect "someOtherNode" from what I understand it will only destroy the pointer? Is this correct and if so is there a way to set what it is POINTING to, to null?Java Code:myNode = null;
Somthing like how C uses:
I dont remember if thats the C syntax but the idea is that null will apply to whatever it is pointing at.Java Code:nodePointer* = null;
Any tips? Thank you!
- 11-04-2008, 02:05 PM #2
You have a problem telling the difference between an object and a pointer. myNode and someOtherNode are both pointers.
No quite.myNode is pointing to someOtherNode not making a copy.
The assignment statement sets myNode to point to the same object as someOtherNode points to.
someOtherNode is a pointer, not an object. You can set its value to null. myNode will still point to the object.I now need to set the "someOtherNode" to null but the only referance I have to it is myNode.
Invalid concept. Objects can't be set to null. Their instance variables can be set to null, but the object will still exist.is there a way to set what it is POINTING to, to null?
Objects are collected by the garbage collector when there are no pointers referencing them.
- 11-04-2008, 07:40 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 43
- Rep Power
- 0
Thank You for the help, I understand now how pointers work. When I needed to delete a node somewhere else in my binary tree, in the algorithm I had a pointer following one node behind and once I found the node I used the previouse node to do somthing like:
This seemed to work well, aside from the tree breaking apart so I also checked to see if it was a leaf node and if it was not I replaced it's values with the approriate node one to the left or the right and then as far right or left from there as I could go. Then if need be re connected the new node that is being removed back up again if it had children on the other side!Java Code:previouseNode.leftNode = null;
I hope I described what I did well enough for my next question...
This all worked and my program is fine but it seems very long and unnessesary, but they only way I can think of. There are so many cases my delete function became quite large and hard to read with lots of if then and while loops. (I'm not allowed to use recursive for this project) Is this a standard meathod? or is there somthing better?
Thank You.
- 11-04-2008, 08:02 PM #4
Honestly I'm sure there is another way to do this(recursion being one) but I think this project could be the introduction to binary trees or something and as long as it works that's fine. Your next project might be to make the code more efficient and you could be asked to use recursion in which case you want to be able to improve your code.
For homework assignments and projects and such I always figured as long as it worked and did what it was supposed to it was fine. They were assignments to teach me an idea, it's not going to be perfect the first time through. Now if this is a real world project that will actually be implemented then yes your probably going to have to enhance it a bit.
- 11-04-2008, 08:30 PM #5
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
Like Norm said, you can have a null reference, but you cannot have a null object. You can add an "isnull" field to your class.
The only reason I would see for you to do this is if there are multiple references to the same object such that a method or thread operating on one of those objects, namely delete it, should affect all other references to it. Though perhaps a more appropriate representation of this would be to make a list of references to specific objects.
Similar Threads
-
Pointers
By ravian in forum New To JavaReplies: 5Last Post: 11-28-2007, 01:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks