2016-04-09 6 views
0

こんにちは私は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; 
    } 

} 
+0

少し明確にするために、私が実行したとき私は結果をSOP(キューをポーリングし、100を期待し、100を得ました。助けてくれてありがとう! – xtremeslice

答えて

1

はルックス:

GGCQueue<Number> ggcQ = new GGCQueue<>(); 
+0

申し訳ありませんこれで何ができますか? – xtremeslice

+0

エラーメッセージ "GGCQueueは生の種類です。" 'GGCQueue 'を使って修正されます。 Numberは 'Integer'、' Double'、 'Float'のスーパータイプです。あなたが100を提供すると、 'int'から' Integer'への自動ボクシングになります。これは 'Number'で、キューで許されます。 Pollは 'Number'を返します。これは、あなたのコードが「期待している100、got:100」という文字列に変換されます。 – AJNeufeld

+0

ありがとうございました! +1。 – xtremeslice

関連する問題