2012-04-11 69 views
0

可能重複:
Java how to: Generic Array creation堆棧泛型類工作不

import java.util.EmptyStackException; 
import java.util.Vector; 

public class Stack<E> extends Vector<E> { 
    private E a[]; 
    private int top; 

    public void Stack() { 
     a = new E[100]; 
     top = -1; 
    } 

    public void Stack(int n) { 
     a = new E[n]; 
     top = -1; 
    } 

    public E pop() { 
     E obj; 
     int len = size(); 
     if (top == -1) 
      throw new EmptyStackException(); 
     else 
      obj = a[top--]; 
     return obj; 
    } 

    public void push(E e) { 
     if (e == null) 
      throw new NullPointerException(); 
     else if (top == size() - 1) 
      System.out.println("Stack full"); 
     else { 
      a[++top] = e; 
      System.out.println("pushed :" + e); 
     } 

    } 

    public int size() { 
     int i; 
     for (i = 0; a[i] != null; i++) 
      ; 
     return i; 
    } 

} 

這是我的籌碼仿製藥Java類。我在兩個構造函數(即Stack()和Stack(int n))中獲得數組聲明中的錯誤。在這兩種情況下,錯誤都是「通用數組創建」。請幫助

+4

請參閱此問題:http://stackoverflow.com/questions/529085/java-how-to-generic-array-creation – Mat 2012-04-11 12:49:13

+0

你的構造函數是不是真的構造函數,因爲構造函數沒有返回類型。另外,你正在擴展'Vector',這意味着你不需要有一個本地數組。 – Paaske 2012-04-11 13:05:55

回答

1

通用數組無法創建。所以使用Object數組。


import java.io.*; 
import java.util.EmptyStackException; 
import java.util.Vector; 
public class Stack extends Vector 
{ 
    private Object a[]; 
    private int top; 
    public void Stack() 
    { 
    a=new Object[100]; 
    top=-1; 
    } 
    public void Stack(int n) 
    { 
    a=new Object[n]; 
    top=-1; 
    } 
    public E pop() 
    { 
    E obj; 
    int len = size(); 
    if (top == -1) 
     throw new EmptyStackException(); 
    else 
     obj=(E) a[top--]; 
    return obj; 
    } 
    public void push(E e) 
    { 
    if(e==null) 
     throw new NullPointerException(); 
    else if(top==size()-1) 
     System.out.println("Stack full"); 
    else 
    { 
     a[++top]=e; 
     System.out.println("pushed :"+e); 
    } 


} 
public int size() 
{ 
    int i; 
    for(i=0;a[i]!=null;i++); 
    return i; 
} 
} 
+0

通用數組當然可以創建。查看其他答案..但使用Object []'也可以。 – Paaske 2012-04-11 13:07:27

+0

Object []是做什麼的?它可以存儲hetrogenous數據嗎? – rick 2012-04-15 21:14:14

1

你的類沒有構造函數,這些都是方法。爲construtors刪除void類型。構造函數沒有返回類型。此外,您的班級名稱以低字母堆棧開始,構造函數以堆棧開始。將你的類重命名爲Stack並刪除void類型。

你需要做的是這樣的

 public Stack(Class<E> cls) 
{ 
     a = (E[]) Array.newInstance(cls); 

    top=-1; 
} 

public Stack(Class<E> cls,int n) 
    { 
    a = (E[]) Array.newInstance(cls,n); 
    top=-1; 
    } 

看到Generic Array

以下是在main方法創建對象的工人階級。

class Stack<E> extends Vector<E> { 
    private E a[]; 
    private int top; 

    public Stack(Class<E> cls) 
    { 


     a = (E[]) Array.newInstance(cls); 

     top=-1; 
    } 

    public Stack(Class<E> cls,int n) 
    { 


     a = (E[]) Array.newInstance(cls,n); 

     top=-1; 
    } 

    public E pop() { 
     E obj; 
     int len = size(); 
     if (top == -1) 
      throw new EmptyStackException(); 
     else 
      obj = a[top--]; 
     return obj; 
    } 

    public void push(E e) { 
     if (e == null) 
      throw new NullPointerException(); 
     else if (top == size() - 1) 
      System.out.println("Stack full"); 
     else { 
      a[++top] = e; 
      System.out.println("pushed :" + e); 
     } 

    } 

    public int size() { 
     int i; 
     for (i = 0; a[i] != null; i++) 
      ; 
     return i; 
    } 

    public static void main(String...strings){ 
     Stack<Integer> st=new Stack<Integer>(Integer.class,n); 
    } 

} 
+0

製作對象時參數是什麼? – rick 2012-04-11 13:04:40

+0

堆棧堆棧=新堆棧(String.class); – Shehzad 2012-04-11 13:09:38

+0

我已編輯我的答案 – Shehzad 2012-04-11 13:12:18