java—将一个对象分配给另一个对象是否意味着即时变量也会改变?

zfycwa2u  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(318)

我想创造一个 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

btxsgosb

btxsgosb1#

“我已经做了所有这些,但我的问题是,当将对象a分配给对象b时,这不意味着a将指向b的地址吗?”
如果这是你的意思:
stacks a=新堆栈();
烟囱b=a;
这意味着b现在指向a。

f0brbegy

f0brbegy2#

你做得太过分了。一个堆栈应该由一个节点链组成,就像一个单一的节点链表。我在下面写了一个例子,看看你能不能看到它是如何工作的。

public class Stack <E> {

    private StackItem<E> currTop;
    private int size;
    private int max;

    private static class StackItem<E> {
        private E e;
        private StackItem<E> next;
    }

    public Stack(int max) {
        currTop = null;
        size = 0;
        this.max = max;

    }

    public void add(E e){
        if ((size+1) == max) throw new StackOverflowError("Max items in stack is reached");
        StackItem<E> old = currTop;             
        currTop = new StackItem<>();           
        currTop.e = e;                          
        currTop.next = old;                     
        size++;                                

    }

    public E getFirst() {
        if (currTop == null) return null;   
        E output = currTop.e;               
        currTop = currTop.next;             
        size --;
        return output;

    }

    public E showFirst() {
        return currTop.e;
    }

    public int getSize() {
        return size;
    }

}
new9mtju

new9mtju3#

在java中,您将对象引用分配给变量。
我已经做了所有这些,唯一的问题是当把对象a分配给对象b时,这不意味着a将指向b的地址吗?

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

反之亦然,因为java中的赋值是从右向左的。

相关问题