1

Map x<Apartment,Vector<Expense>>如果我@override使用 Map 方法,我需要初始化它允许我

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

但是我在网上看到 Map 不是一个接口,它是一个类,是我做错了,还是必须这样做?

4

3 回答 3

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

Map是一个接口(正如javadoc所说——javadoc 应该是您的参考,而不是 Internet)。您需要选择一种实现(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 下。

于 2012-11-25T13:16:18.740 回答
3

但是我在互联网上读到 Map 不是界面

获取有关编程语言信息的最佳位置始终是官方文档。说到java,请务必先看看:http ://docs.oracle.com/javase/7/docs/

地图是一个界面。来自 Java 文档 API:

将键映射到值的对象。地图不能包含重复的键;每个键最多可以映射到一个值。这个接口代替了 Dictionary 类,它是一个完全抽象的类,而不是一个接口。

我建议您使用HashMap等实现类之一。来自 Java API 文档:

Map 接口的基于哈希表的实现。此实现提供所有可选的映射操作,并允许空值和空键。(HashMap 类大致相当于 Hashtable,除了它是不同步的并且允许空值。)这个类不保证映射的顺序;特别是,它不保证订单会随着时间的推移保持不变。

您的代码可以通过简单地替换Mapfor来运行HashMap,如下所示:

new HashMap<Apartment,Vector<Expense>>

我希望它有所帮助。干杯

于 2012-11-25T13:16:34.497 回答
1

请在此处查看:http: //docs.oracle.com/javase/6/docs/api/java/util/Map.html

您可以使用 HashMap 进行实例化。

于 2012-11-25T13:16:20.840 回答