将对象分配给另一个对象意味着即时变量是否会发生变化?

时间:2017-07-14 12:09:20

标签: java oop pass-by-reference

我正在尝试创建一个stacks,其中包含以下API:

Stacks(int n)// creates stacks of size n

pop() //returns the last element pushed in the stacks 

pop(int n) //returns an array of of n elements 

push(int e) //appends an element to the stacks

push(int n, ar[]) //appends an array to the stack

堆栈应该能够在需要时动态地改变大小,因此客户端程序不必每次都这样做。

我已经做了所有这一切,只有在将对象A分配给对象B时我的问题并不意味着A现在将指向B的地址?

这是我的代码,我希望它能解释我的意思

public class Stacks {
    /*
     * constructs a stack object
     * @param n that will determine that size of the stacks to be constructed
     */
    public Stacks(int n)
    {
        this.elemetns= new int[n];
        this.size=n;
        this.top=-1;
    }
    /*
     * constructs a stack object, with size of 2 when no parameter is given
     */
    public Stacks()
    {
        this.elemetns= new int[2];
        this.size=2;
        this.top=-1;
    }

    public int pop()
    {
        if (top<0)
        {
            System.out.println("Error code 2: Empty stacks");
            return -1;
        }
        else
            {
                int n= this.elemetns[top];
                top--;
                return n;
            }
    }
    public int [] pop(int size)
    {
        if (this.size<size)
        {
            System.out.println("Error code 3: The Maximum number of elements that can be acquired is "+ this.size);
            return null;
        }
        else 
        {
            int res[]= new int[size];
            for (int i=0;i<size;i++)
            {
                res[i]=pop();
            }
            return res;
        }
    }
    public void push(int e)
    {
        if (!isFull())
        {
            this.elemetns[++top]=e;
            System.out.println(e+" has been pushed to the stack ");
        }
        else
        {
            updateStacksSize(this);
            this.elemetns[++top]=e;
            System.out.println(e+" has been pushed to the stack ");
        }

    }
    public void push(int n,int [] ar)
    {
        for (int i=0;i<n;i++)
            this.push(ar[i]);
    }
    private void updateStacksSize(Stacks s)
    {
        int newSize= s.top*2;
        Stacks newStacks= new Stacks(newSize);
        for (int i = s.top; i>-1;i--)
            newStacks.elemetns[i]=s.pop();
        s= newStacks;//shouldnt newStacks get garbage collected
//and s gets the new address and attributes of  newStacks?

    }
    private boolean isFull(){return this.size==(this.top+1);}



    public static void main(String[] args)
    {
        Stacks s= new Stacks(5);
        for (int i=0;i<7;i++)
            s.push(i+1);
        System.out.println();
        int []arr= s.pop(6);
        for (int i=0;i<arr.length;i++){
            System.out.println(arr[i]);
        }
    }
    private int elemetns[];
    private int top;
    private int size;
}

为什么运行此程序会导致旧版本的问题,尽管当前对象已更新。

还有一个问题是可以分配this= newStacks而不是实例化新的Stacks object

3 个答案:

答案 0 :(得分:0)

在Java中,您可以为变量分配对象引用。

  

我已经做了所有这一切,只有我的问题是在将对象A分配给对象B并不意味着A现在指向B的地址?

   s= newStacks;//shouldnt newStacks get garbage collected
   //and s gets the new address and attributes of  newStacks?

这是另一种方式,因为Java中的赋值是从右到左。

答案 1 :(得分:0)

&#34;我完成了所有这一切,只有我的问题是将对象A分配给对象B并不意味着A现在将指向B的地址?&#34;

如果这是你的意思那么:

Stacks A = new Stacks();

堆叠B = A;

那么这意味着B现在指向A。

答案 2 :(得分:0)

你有点过头了。堆栈应该由一系列节点组成,就像一个串联的节点列表。我在下面写了一个例子,看看你是否能看到它是如何工作的。

spring-boot.jar