2017-08-16 84 views
-7
public class StackSimple{ 
    private long capacity=1000;//maximum size of array 
    private int idx_top; 
    private Object data[]; 

    public StackSimple(int capacity) 
    { 
     idx_top=-1; 
     this.capacity=capacity; 
     data = new Object[capacity]; 
    } 

    public boolean isEmpty(){ 
     return(idx_top<0); 

    } 
    public boolean isFull(){ 
     return(idx_top>=capacity-1); 
    } 

    public int size() 
    { 
     return idx_top+1; 
    } 

    public boolean push(Object x){ 
     if (isFull()){ 
      throw new IllegalArgumentException("ERROR:Stack Overflow.Full Stack"); 
} 
     else 
     {`enter code here`data[++idx_top]=x; 
      return true; 
     } 
    } 

    public Object pop(){ 
     if(isEmpty()) 
      throw new IllegalArgumentException("ERROR:Stack Underflow.Empty Stack."); 
     else{ 
      return data[idx_top--]; 
     } 
} 

public Object top(){ 
    if (isEmpty()) 
     throw new IllegalArgumentException("ERROR:Stack Underflow.Empty Stack."); 
    else{ 
     return data[idx_top]; 
    } 
} 

public void print() 
{` 
for (int i=size()-1;i>=0;i--) 
     System.out.println(data[i]); 
} 



} 

public class Stack_Exercise { 
    public static void main(String[] args) { 
     StackSimple s = new StackSimple(capacity:3);//error shows here 
     s.push(x:"books");`enter code here` 
     s.push(x:"something"); 
     s.push(x:"200"); 
     s.print(); 
     System.out.println("Size=" +s.size()); 
    } 
} 

爲什麼不能正常工作? 爲什麼在創建StackSimple對象時說無效語句?運行它時,問題在主類中。推動元素時有錯誤。這段代碼有什麼問題?它不會運行

Error while compiling

+2

'new StackSimple(capacity:3)'?? ??只是'新的StackSimple(3)'。你是否偶然使用IntelliJ? –

+2

把你的錯誤信息,而不是一張圖片。 – aristotll

+0

這對你意味着什麼?:*** StackSimple(capacity:3); *** –

回答

0

當參數傳遞給一個函數,你只是傳遞的價值觀。 在你的情況下不是StackSimple(capacity:3)而只是StackSimple(3)

0

第一個問題,你正在使用哪個版本的Java。其次,在Java中,你應該作爲一個變量而不是StackSimple(容量:3)傳遞。你的主要方法更改爲下面,這裏是我的建議:

StackSimple s = new StackSimple(3); 
    s.push("books"); 
    s.push("something"); 
    s.push("200"); 
    s.print(); 
    System.out.println("Size=" +s.size()); 
+0

我正在使用java 8.此代碼由我們的講師提供。我試圖研究和運行它,但不能。 – Rakshya

+0

謝謝。但它仍然無法正常工作。 – Rakshya

0

你是不是在所有的堆棧推值,因爲預計到你的工作PUSCH功能無法正常工作。

這是正確的程序。

class StackSimple { 
    private long capacity = 1000;// maximum size of array 
    private int idx_top; 
    private Object data[]; 

    public StackSimple(int capacity) { 
     idx_top = -1; 
     this.capacity = capacity; 
     data = new Object[capacity]; 
    } 

    public boolean isEmpty() { 
     return (idx_top < 0); 

    } 

    public boolean isFull() { 
     return (idx_top >= capacity - 1); 
    } 

    public int size() { 
     return idx_top + 1; 
    } 

    public boolean push(Object x) { 
     if (isFull()) { 
      throw new IllegalArgumentException("ERROR:Stack Overflow.Full Stack"); 
     } else { 
      data[++idx_top] = x; 
      return true; 
     } 
    } 

    public Object pop() { 
     if (isEmpty()) 
      throw new IllegalArgumentException("ERROR:Stack Underflow.Empty Stack."); 
     else { 
      return data[idx_top--]; 
     } 
    } 

    public Object top() { 
     if (isEmpty()) 
      throw new IllegalArgumentException("ERROR:Stack Underflow.Empty Stack."); 
     else { 
      return data[idx_top]; 
     } 
    } 

    public void print() { 
     for (int i = size() - 1; i >= 0; i--) 
      System.out.println(data[i]); 
    } 

} 

public class test { 
    public static void main(String[] args) { 
     StackSimple s = new StackSimple(3);// error shows here 
     s.push("books"); 
     s.push("something"); 
     s.push("200"); 
     s.print(); 
     System.out.println("Size=" + s.size()); 
    } 
}