に示すように、次のメソッドを使用してこのスタック クラスを作成しています。
import java.util.ArrayList;
import java.util.EmptyStackException;
public class SortableStack<E extends Comparable<E>> implements ISortableStack<E> {
private int N;
private Node first;
private class Node {
private E e;
private Node next;
}
public SortableStack() {
first = null;
N = 0;
}
private ArrayList<E> listOne = new ArrayList<E>();
public boolean isEmpty() {
return first == null;
}
public int size() {
return N;
}
public void push(E e) {
Node oldfirst = first;
first = new Node();
first.e = e;
first.next = oldfirst;
N++;
}
public E pop() {
if (isEmpty()) throw new RuntimeException("Stack underflow");
E e = first.e; // save e to return
first = first.next; // delete first node
N--;
return e; // return the saved e
}
public E peekMidElement() {
if(listOne.size() <= 0){
throw new EmptyStackException();
}
return listOne.get(listOne.size()/2);
}
public E peekHighestElement() {
if(listOne.size() <= 0){
throw new EmptyStackException();
}
return listOne.get(listOne.size() - 1);
}
public E peekLowestElement() {
if(listOne.size() <= 0){
throw new EmptyStackException();
}
return listOne.get(0);
}
}`
//インターフェイス ISortableStack は [here][1] です (コメントは、必要なメソッド シグネチャを説明しています)。
[1] :http://stackoverflow.com/questions/7130901/Java-stack-implementation
ここで、本体クラスを作成しようとすると、次のようになります。
import java.io.*;
public class ExhibitStack<E extends Comparable<E> > {
E ch;
public static void main(String[] args) throws IOException {
ISortableStack<E> s = new ISortableStack(5); // Cannot instatiate ISORTABLESTACK
ExhibitStack demo = new ExhibitStack();
// Cannot make reference to a non static type
while ((demo.ch = (E) System.in.read()) != '\n') {
if (!s.full()) {
s.push(demo.ch);
}
}
while (!s.empty()) {
System.out.print(s.pop());
}
System.out.println();
}
}
ISortableStack で次のようにエラーをスローします: 非静的型への参照を作成できません。ISORTABLESTACK を起動できません
インターフェイスを使用してメニュー駆動型のプログラムを作成したいと考えています。私は Java GENERICS とコレクションが苦手で、課題の提出がかなり遅れています。ヘルプ/指示をいただければ幸いです。