2010-07-10 79 views
0

我想編譯這個程序。它適用於2個字符串(姓名,電話號碼),但不適用於3個字符串(姓名,電話號碼和性別)。使用java.util.Map時的編譯問題


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個字符串(姓名,PHONENUMBER)


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屬性工作,電話,性別,年齡,地址。如果有人能夠幫助我編譯問題頂部的代碼,我可以找出其餘的問題。

感謝

+0

當在泛型聲明中放置不正確的對象時,我遇到了此錯誤。它應該是'>',但實際上是>。這令人困惑,因爲編譯器沒有指出該行的正確位置。這是函數簽名中的許多參數之一,編譯器指向該行的開頭。 – aliteralmind 2014-06-26 15:32:58

回答

7

你不能只是隨意添加一個類型參數的泛型類型 - 它們與一定數量的定義,並有能力使用很多(不考慮原始類型)。類型參數對實現具有特定的含義 - 如果你叫map.get(name)HashMap類將如何知道你想要得到什麼?

您應該將所有屬性封裝到一個類中(例如,PersonContact),然後創建一個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);