0

このプログラムをコンパイルしようとしています。2つの文字列(名前、電話番号)では完全に機能しますが、3つの文字列(名前、電話番号、性別)では機能しません。


CODE機能しないコード-3つの文字列(名前、電話番号、性別)


import java.util.Map;
import java.util.TreeMap;

public class Ann {

String name, phone;

public Ann() {
}

public static void testMap() {
    Map<String, String, String> theMap = new TreeMap<String, String,String>();
    // new HashMap<K,V>(); could also be used
    theMap.put("Roger M", "090-997-2918", "Male");
    theMap.put("Jane M", "090-997-1987", "FeMale");
    theMap.put("Stacy K", "090-997-9188", "FeMale");
    theMap.put("Gary G", "201-119-8765", "Male");
    theMap.put("Jane M", "090-233-0000", "FeMale");
    System.out.println("Testing TreeMap and Map");
    System.out.print("Stacy K has phone ");
    System.out.print(theMap.get("Stacy K"));
    System.out.print("\n");

    System.out.print("Jane M has phone ");
    System.out.print(theMap.get("Jane M"));
} // testMap()

public static void main(String[] args) {
    testMap();

}
}

エラー

wrong number of type arguments; required 2

wrong number of type arguments; required 2


作業コード2つの文字列(名前、電話番号)の場合


import java.util.Map;
import java.util.TreeMap;

public class Ann {

String name, phone;

public Ann() {
}

public static void testMap() {
    Map<String, String> theMap = new TreeMap<String, String>();
    // new HashMap<K,V>(); could also be used
    theMap.put("Roger M", "090-997-2918");
    theMap.put("Jane M", "090-997-1987");
    theMap.put("Stacy K", "090-997-9188");
    theMap.put("Gary G", "201-119-8765");
    theMap.put("Jane M", "090-233-0000");
    System.out.println("Testing TreeMap and Map");
    System.out.print("Stacy K has phone ");
    System.out.print(theMap.get("Stacy K"));
    System.out.print("\n");

    System.out.print("Jane M has phone ");
    System.out.print(theMap.get("Jane M"));
    } // testMap()

public static void main(String[] args) {
    testMap();

}
}

名前、電話、性別、年齢、住所など、約5つの属性でコードを機能させたいです。誰かが質問の上部にあるコードをコンパイルするのを手伝ってくれるなら、残りを理解することができます。

ありがとう

4

1 に答える 1

8

ジェネリック型に型パラメーターを任意に追加することはできません。それらは特定の数で定義され、その数を使用する必要があります(生の型は無視されます)。タイプパラメータには、実装に対して特定の意味があります。HashMap呼び出した場合、クラスはどのようにして何を取得したいかを知ることができますmap.get(name)か?

Personすべてのプロパティをクラス(またはContact)にカプセル化してMap<String, Person>から、名前から人物にマップするためのを作成する必要があります。例えば:

public enum Gender
{
    FEMALE, MALE;
}

public final class Person
{
    private final String name;
    private final Gender gender;
    private final Date dateOfBirth;
    private final String address;
    private final String telephone;

    public Person(String name, Gender gender, Date dateOfBirth,
                  String address, String telephone)
    {
        // You probably want to put some validation in here
        this.name = name;
        this.gender = gender;
        this.dateOfBirth = dateOfBirth;
        this.address = address;
        this.telephone = telephone;
    }

    public String getName()
    {
        return name;
    }

    // etc for the other properties
}

...

Map<String, Person> map = new HashMap<String, Person>();
Person jon = new Person("Jon", Gender.MALE, /* etc */);
map.put("Jon", jon);
于 2010-07-10T07:55:09.073 に答える