2012-11-25 65 views
1

我需要初始化一個像Map x<Apartment,Vector<Expense>>它可以讓我,如果我@override地圖方法初始化一個Map <富,矢量<Bar>> - Java 7的

Description Resource Path Location Type 
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.isEmpty() Repository.java /proj_individual/src/repo line 12 Java Problem 
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.size() Repository.java /proj_individual/src/repo line 12 Java Problem 
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.keySet() Repository.java /proj_individual/src/repo line 12 Java Problem 
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.remove(Object) Repository.java /proj_individual/src/repo line 12 Java Problem 
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.entrySet() Repository.java /proj_individual/src/repo line 12 Java Problem 
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.putAll(Map<? extends Apartment,? extends Vector<Expense>>) Repository.java /proj_individual/src/repo line 12 Java Problem 
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.values() Repository.java /proj_individual/src/repo line 12 Java Problem 
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.clear() Repository.java /proj_individual/src/repo line 12 Java Problem 
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.get(Object) Repository.java /proj_individual/src/repo line 12 Java Problem 
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.containsKey(Object) Repository.java /proj_individual/src/repo line 12 Java Problem 
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.containsValue(Object) Repository.java /proj_individual/src/repo line 12 Java Problem 
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.put(Apartment, Vector<Expense>) Repository.java /proj_individual/src/repo line 12 Java Problem 

但我在互聯網上閱讀地圖不是接口,它是一個類,我做錯了,或者這是必須完成的方式?

+0

嗯..什麼? '地圖是一類' - 你在互聯網上讀到這個? –

+0

地圖是一個接口。 (http://docs.oracle.com/javase/6/docs/api/java/util/Map.html) –

+1

怎麼樣'地圖<公寓,矢量> myMap = new HashMap <公寓,矢量>( )'?並看看JB Nizets的答案。你可以找到你需要的所有信息。 – semTex

回答

7
Map<Apartment, Vector<Expense>> map = new HashMap<Apartment, Vector<Expense>>(); 

Map是一個接口(如the javadoc說— javadoc的應該是你的參考,而不是互聯網)。你需要選擇一個實現(HashMap是最常用的實現,但還有其他實現,以及其他特性)。

你也應該忘記Vector。它不應該被使用了。使用List作爲一個類型,和ArrayList作爲實現(也有其他的List實現,但ArrayList中幾乎總是你想要的):

Map<Apartment, List<Expense>> map = new HashMap<Apartment, List<Expense>>(); 

或者乾脆

Map<Apartment, List<Expense>> map = new HashMap<>(); 

,如果你是下Java 7中

+0

Java 7,不妨使用'<>'運算符 – darrengorman

+0

我在我的答案中加入了這個。謝謝。 –

+0

Ty,對不起,如果answeres是傻瓜,但只是習慣了java,有時我不知道我在哪裏可以找到我需要的東西 –

3

不過,我看到在互聯網上說地圖是不是一個接口

約一門編程語言永遠是官方文檔信息的最佳場所。說到java,只要確保先看看:http://docs.oracle.com/javase/7/docs/

Map是一個接口。從Java文檔API:

將鍵映射到值的對象。地圖不能包含重複的鍵;每個鍵可以映射到最多一個值。 這個接口取代了Dictionary類,它是一個完全抽象的類而不是接口。

我建議您使用其中一個實施類,如HashMap。從Java API文檔:

基於哈希表的Map實現的接口。該實現提供了所有可選的映射操作,並允許使用空值和空鍵。 (HashMap類大致等同於Hashtable,除了它是不同步的並且允許爲空值。)這個類不能保證地圖的順序;特別是,它不能保證訂單會隨着時間的推移保持不變。

您的代碼可以通過簡單地更換MapHashMap在被然後是功能:

new HashMap<Apartment,Vector<Expense>> 

我希望它幫助。乾杯

相關問題