2017-07-14 69 views
0

我试图创建一个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的地址?

这里是我的代码,我希望它explaines我的意思

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

+1

没有在Java中分配'this'是不可能的。 'this'只能由JVM分配一次,并且它实际上是一个对象的最终变量。 –

+0

您分配给本地变量/参数,不会修改调用者的变量... –

+0

另外** Java集合框架**中已经有堆栈/队列,请参阅:http://docs.oracle.com/ javase/8/docs/api/java/util/Deque.html –

回答

0

在Java中分配给变量的对象引用。

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

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

它是周围的其他方法,因为Java中的赋值是从右到左。

+0

是啊那是什么我的意思是原始对象的属性是'A'没有改变,如果你运行我的代码我想你会理解我在说什么! – Reddevil

0

“我已经完成了所有只有我的问题是将对象A分配给对象B并不意味着A现在将指向B的地址吗?”

如果是这样,那么你的意思:

栈A =新的堆栈();

堆栈B = A;

那么这是什么意思是B现在指向A.

0

你有点过分了。一个堆栈应该由一系列节点组成,比如一个singel链接的节点列表。我在下面写了一个例子,看看你是否能看到它是如何工作的。

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; 
    } 




} 
+0

抱歉,我对其他解决方案不感兴趣,但我对调试当前的解决方案感兴趣。无论如何感谢您的努力 – Reddevil

相关问题