Results 1 to 8 of 8
- 06-28-2011, 02:39 AM #1
Assign a new Reference for an Object
Hello Friends.
Please consider the following code:
Notice the line of code that I have made bold. There I was attempting to copy/clone the Stack [s] to a different variable [opWrongWay] ! However it isn't working.Java Code://Finding shortest possible path. int sizeOfStack = potentialPaths.get(0).size(); Stack<Node<Adjustment>> opWrongWay = new Stack<Node<Adjustment>>(); for(Stack<Node<Adjustment>> s : potentialPaths) { if(s.size() < sizeOfStack) { sizeOfStack = s.size(); [B]opWrongWay = s;[/B] } }
[opWrongWay] gives me an empty stack (where [s] has elements in).
So my question is, how do I hand over the stack [s] to a different variable (thus giving that variable the same stack content) whilst memory reference wise, keeping the two totally separate and thus in effect creating two objects. Is there an easy way to do this ?
I mean I know that I can iterate through [s] and copy the stuff to [opWrongWay] one by one, but surely there's an easier way ! no ?
I'd greatly appreciate any help.
Thank You.>> What can be asserted without proof can be dismissed without proof. <<
- 06-28-2011, 02:44 AM #2
You will need to clone all the elements in one stack before you add them to the other stack. If your Adjustment class holds objects of another class then you will need to do a deep clone. That is clone all the objects in the objects you are cloning as well.
- 06-28-2011, 03:02 AM #3
I never really play with the <Node<Adjustment>> objects. They're always the same, I just move them about.
My concern is only the Stack. Are you saying the only way is to iterate through the stack and copy each object at a time ? If not .. can you show me what you mean in code please.
I find it very difficult (as some of you guys noticed already) to deduce code from written text.
>> What can be asserted without proof can be dismissed without proof. <<
- 06-28-2011, 03:05 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
It should be noted that - as you describe it - you are not trying to create a new reference to some object. The problem is that the assignment
is just that: an assignment. It gives opWrongWay the very same value as s has. That value being a reference to a stack.Java Code:opWrongWay = s;
If you want another stack (rather than another variable whose value is a reference to the same stack) then there no other way but to create another stack and the put stuff into it (possibly the same stuff that is already in s).
- 06-28-2011, 03:10 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
This ("memory reference wise") is a bit mysterious. Perhaps it involves creating a new stack and cloneing the contents (as Junky suggests) but perhaps not.how do I hand over the stack [s] to a different variable (thus giving that variable the same stack content) whilst memory reference wise, keeping the two totally separate
It would clear things up if you posted some brief runnable code illustrating what the problem is.
- 06-28-2011, 03:58 AM #6
What you're claiming is happening doesn't make sense. If the Stack referred to by s contains elements, then after the assignment opWrongWay = s, the stack referred to by opWrongWay will also contain elements. You're not copying or cloning anything; both variables refer to the same object.
I suspect that what's actually happening is your if statement expression s.size() < sizeOfStack is never true.
Here's a quick way to find out. Your assignment of a new empty Stack to opWrongWay seems unnecessary, if you're just going to assign something else to the same variable. So change this code:
To this:Java Code:Stack<Node<Adjustment>> opWrongWay = new Stack<Node<Adjustment>>();
If after iterating over potentialPaths, opWrongWay is still null, then I'm right about the if statement.Java Code:Stack<Node<Adjustment>> opWrongWay = null;
Get in the habit of using standard Java naming conventions!
- 06-28-2011, 06:35 PM #7
Yep, that is Exactly what's happening.
But, What I want to happen is, I want to take the content of stack [s] and give it to another stack [opWrongWay]. Like I mentioned in my first post, I am aware that this can be done by going through the stack one element at a time, and transfering it to another stack. But I thought there might be some more efficient way. Apparently not !
Thank You All so much for the help
>> What can be asserted without proof can be dismissed without proof. <<
- 06-28-2011, 08:24 PM #8
I think all the Collections classes have constructors that take an argument of the same type. So the easiest way to clone your Stack 's' would be
Edit: Guess not... Stack doesn't have a copy constructor.Java Code:opWrongWay = new Stack<Node<Adjustment>>(s);
Edit2: The docs recommend using LinkedList or another class that implements Deque in place of Stack. LinkedList does have a copy constructor.Last edited by kjkrum; 06-28-2011 at 08:31 PM.
Get in the habit of using standard Java naming conventions!
Similar Threads
-
Assign object to another object?
By Marvin in forum New To JavaReplies: 6Last Post: 04-17-2011, 07:18 PM -
object and reference
By aizen92 in forum New To JavaReplies: 11Last Post: 04-01-2011, 08:39 PM -
How to assign unique id to each object created from same class
By srisar in forum New To JavaReplies: 2Last Post: 02-18-2010, 05:26 PM -
Object and reference
By katie in forum New To JavaReplies: 2Last Post: 10-19-2009, 03:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks