2

I though in java, when you do object1=object2, you will copy the reference of object2 to object1, so object1 and object2 point" to the same object. Is it right?

I wrote a function to sort a stack.

import java.util.Stack;

public class sortStack {

    public static void main(String[] str) {
        Stack<Integer> s = new Stack<Integer>();
        s.push(2);
        s.push(21);
        s.push(43);
        s.push(3);
        s.push(87);
        s.push(2);
        s.push(12);
        s.push(10);
        s.push(25);
        sortStack(s);

        while (!s.isEmpty()) {
            System.out.print(s.pop() + " ");
        }
    }

    public static void sortStack(Stack<Integer> src) {
        Stack<Integer> dst = new Stack<Integer>();
        Stack<Integer> buf = new Stack<Integer>();//buffer

        while (!src.isEmpty()) {
            int v = src.pop();
            if (dst.isEmpty()) {
                dst.push(v);
            }

            while (!dst.isEmpty() && dst.peek() > v) {
                buf.push(dst.pop());
            }

            dst.push(v);
            while (!buf.isEmpty()) {
                dst.push(buf.pop());
            }
        }
        src = dst;

        //Print:
        //while(!src.isEmpty()){
        //  System.out.print(src.pop()+" ");
        //}
    }
}

I couldn't get any output from the class. If I uncomment the print part, it words fine. I don't understand the s stack is empty after I call the function. I already assign dst to s, so s should point to the dst stack, right?

Please help! Thanks!

3
  • A stack is normally used for FILO operations (first-in-last-out). Why not use simple sortable collections like ArrayList? Commented Jan 5, 2014 at 6:06
  • 2
    Relevant: Java is not pass-by-reference. It's strictly pass by value, only some values happen to be object references. stackoverflow.com/questions/40480/is-java-pass-by-reference Commented Jan 5, 2014 at 6:07
  • @MenoHochschild The question not allow to use ArrayList. It's like an Interview question. Commented Jan 5, 2014 at 6:10

2 Answers 2

9

You are assigning a reference to a parameter, src, which is in fact a local variable, and hence you're not seeing any effect on the original Stack variable, s. Instead have the method return dst and assign the object returned. i.e.,

public static Stack<Integer> sortStack(Stack<Integer> src) {
    // ....

   return dst;
}

and

s = sortStack(s);
Sign up to request clarification or add additional context in comments.

Comments

6

When you set src=dst you are only overwriting the local copy of the reference within the method. In the process of calling the method local copies are made of each parameter, such that within your method modifying src through its methods would have an effect outside the method but assigning src to a new reference like dst would not.

Your code will work if you return dst in sortStack and put s = sortStack(s) in your calling function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.