2017-07-14 67 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

相關問題