Results 1 to 8 of 8
  1. #1
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default Assign a new Reference for an Object

    Hello Friends.

    Please consider the following code:

    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]
        }
    }
    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.

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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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.

  3. #3
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    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.

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

    Default

    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

    Java Code:
    opWrongWay = s;
    is just that: an assignment. It gives opWrongWay the very same value as s has. That value being a reference to a stack.

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

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

    Default

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

    It would clear things up if you posted some brief runnable code illustrating what the problem is.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    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:

    Java Code:
    Stack<Node<Adjustment>> opWrongWay = new Stack<Node<Adjustment>>();
    To this:

    Java Code:
    Stack<Node<Adjustment>> opWrongWay = null;
    If after iterating over potentialPaths, opWrongWay is still null, then I'm right about the if statement.
    Get in the habit of using standard Java naming conventions!

  7. #7
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Quote Originally Posted by kjkrum View Post
    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.
    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

  8. #8
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    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

    Java Code:
    opWrongWay = new Stack<Node<Adjustment>>(s);
    Edit: Guess not... Stack doesn't have a copy constructor.

    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

  1. Assign object to another object?
    By Marvin in forum New To Java
    Replies: 6
    Last Post: 04-17-2011, 07:18 PM
  2. object and reference
    By aizen92 in forum New To Java
    Replies: 11
    Last Post: 04-01-2011, 08:39 PM
  3. Replies: 2
    Last Post: 02-18-2010, 05:26 PM
  4. Object and reference
    By katie in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 03:45 PM

Posting Permissions

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