こんにちは私はEclipsesのエラーメッセージを削除するために私のコードをリファクタリングするキューを作ろうとしています: "GGCQueueは生の型です。私は10以上の要素を含むことは決してないキューを作成したい。私はこれを「一般的な」原則にしたがって実行しようとしていますが、現時点ではそれをどうやって行うのか分かりません。私は(私はこれを実装する必要がどこのクラスでコンストラクタGGCQueueは()であるGGCQueue)次のコードを持っている:あなたが望むように小さなキューを作成する(ジェネリック)
import java.util.LinkedList;
import java.util.List;
public class GenericsProblem {
public static void main(String[] args) {
GGCQueue ggcQ = new GGCQueue();
ggcQ.offer(100);
ggcQ.offer(1000);
ggcQ.offer(3.33D);
ggcQ.offer(9);
ggcQ.offer(7);
ggcQ.offer(9.001F);
System.out.println("polling the queue, expecting 100, got:" + ggcQ.poll());
System.out.println("polling the queue, expecting 1000, got:" + ggcQ.poll());
System.out.println("polling the queue, expecting 3.33, got:" + ggcQ.poll());
System.out.println("polling the queue, expecting 9, got:" + ggcQ.poll());
System.out.println("polling the queue, expecting 7, got:" + ggcQ.poll());
System.out.println("polling the queue, expecting 9.001, got:" + ggcQ.poll());
}
}
class GGCQueue<E> {
List<E> q = new LinkedList<E>();
public GGCQueue() {
//TODO MAKE THE SMALL QUEUE <= 10 ELEMENTS
}
public void offer(E element) {
if (q.size() == 10)
return;
q.add(element);
}
public E poll() {
E element = q.remove(0);
return element;
}
}
少し明確にするために、私が実行したとき私は結果をSOP(キューをポーリングし、100を期待し、100を得ました。助けてくれてありがとう! – xtremeslice