2017-03-23 7 views
-1

ジェネリックスを使用してコレクション用の小さなコードを書いていましたが、そのコードは以下のとおりです。Javaジェネリックスを学ぶ

import java.util.*; 

// one class needs to have a main() method 
public class HelloWorld 
{ 
    // arguments are passed using the text field below this editor 
    public static void main(String[] args) 
    { 
    List<?> list1 = new ArrayList<?>(); 

    list1.add("Zahid"); 
    list1.add(22); 

    System.out.print(list1.toString()); 
    } 
} 

誰かがこのコードの何が問題なのか説明できます。 以下のエラーが発生しています。

error: unexpected type                           
    List<?> list1 = new ArrayList<?>();                            
           ^                            
    required: class or interface without bounds                          
    found: ?                                  
HelloWorld.java:11: error: no suitable method found for add(String)                     
    list1.add("Zahid");                                
     ^                                  
    method Collection.add(CAP#1) is not applicable                         
     (argument mismatch; String cannot be converted to CAP#1)                      
    method List.add(CAP#1) is not applicable                          
     (argument mismatch; String cannot be converted to CAP#1)                      
    where CAP#1 is a fresh type-variable:                            
    CAP#1 extends Object from capture of ?                           
HelloWorld.java:12: error: no suitable method found for add(int)                     
    list1.add(22);                                 
     ^                                  
    method Collection.add(CAP#1) is not applicable                         
     (argument mismatch; int cannot be converted to CAP#1)                       
    method List.add(CAP#1) is not applicable                          
     (argument mismatch; int cannot be converted to CAP#1)                       
    where CAP#1 is a fresh type-variable:                            
    CAP#1 extends Object from capture of ?                           
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output               
3 errors 
+0

あなたはジェネリック型を指定する必要があります( '一覧')または「許可」のすべてのタイプの '一覧<と:

は、ワイルドカードの詳細を知るには?オブジェクト>を拡張します。しかし、第2版は利点がないので、最初の版を好むべきです。 –

+0

@PJvGリスト list1 = new ArrayList ();をコンパイルすることはできますが、Slimuは無限のワイルドカードタイプのリストに 'null 'を追加することしかできないと答えました。 –

+0

@StefanWarminski右、私は間違った言い方で、私は彼ができないことを意味しました。リスト list1 = new ArrayList (); 'list1.add(" Zahid ");'が続きます。 Slimuの答えが問題を説明するのに十分であるため、コメントを削除します。 – PJvG

答えて

1

バインドされていないワイルドカードタイプの汎用コレクションを使用しています。つまり、そのコレクションを反復することができ、追加できる唯一の値はnullです。

あなたはより多くを学びたい場合は、このチュートリアルを参照してください: https://docs.oracle.com/javase/tutorial/java/generics/

0

あなたは

新しいArrayListの<を言っているとき>();

基本的に何をしようとしているのは、ArrayListをインスタンス化することです。

ワイルドカード文字の[?]。 wildCard [?]は、特定の未知の型を意味します。そのタイプはコンパイル時には分かっていないので、リストを変更することはできません。

java.utilでarrayListの実装が見られる場合は、arrayList内にGenericsがあり、型が必要です。

したがって、ワイルドカード "?"を使用すると、コンパイラはインスタンス化するArrayListの型を知らない。コンパイル時エラー "ArrayListの型をインスタンス化できません"

何かを追加しているときにエラーが発生します。リストにnullを追加するよう求められます。 [https://stackoverflow.com/a/12341269/7756013]