有人可以幫我找出爲什麼我的通用堆棧不接受整數值?我收到一個編譯錯誤,指出:意外類型。通用堆棧不接受整數值
import java.util.Scanner;
public class Application {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
int phoneNumber;
int rightDigit;
GenericStack<int> digits = new GenericStack<int>();
System.out.print("Enter Your phone number: ");
phoneNumber = sc.nextInt();
while (phoneNumber !=0)
{
rightDigit = phoneNumber % 10;
digits.push(rightDigit);
phoneNumber /= 10;
}
System.out.print("Your phone number is: ");
for(int i=0; i< digits.size(); i++)
System.out.print(digits.get(i));
System.out.println();
}
}
這裏是通用堆棧類:
import java.util.LinkedList;
public class GenericStack<E> {
private LinkedList<E> digitsList = new LinkedList<E>();
public void push(E digit) {
digitsList.addFirst(digit);
}
public int get(E index) {
return digitsList.get(index);
}
}
任何幫助,將不勝感激。
謝謝@DaveRlz –