实现基于 Arraylist 的 Bag/Multiset——卡在泛型上
作业要求我们使用 ArrayList 或 LinkedList 作为后端来实现 Bag 数据类型。我选择了 ArrayList 并创建了我自己的实现,包括 Collection 接口的本地实现。现在,我正在努力解决如何使用我的 List 类调整 Bag 的属性的基本概念,特别是在列表中插入列表。
我的 List 的设置方式遵循通常的 Java 类型,只是没有实现所有方法,这就是为什么我编写了自己的 Collection 接口类型来
public interface Collection<E>
实现 add、remove、size、clear、contains、isEmpty 和 toArray 方法。
List 类型声明为
public class List<E> implements Collection<E>
具有三个私有字段:
private int size;
private E[] data;
private int growSize=10;
E[] data
保存所需的任何元素类型,growSize 是根据需要增加列表大小的新槽的数量。当列表传统上用于单个对象时,这非常有效,但当我尝试插入列表类型时,我会收到错误。
我正在尝试实现这个方法:
public boolean add(Collection<E> c){
if (size < data.length){
data= (E[]) new Object[10];
data[0]= c;
}
}
当我尝试这个时,它说 Collection 和 E 之间存在类型不匹配。
两个问题:
- 首先,我在这里违反了 Java 泛型的哪些规则,为什么?
- 其次,需要什么来修复它?
An assignment has asked us to implement a Bag data type using either ArrayList or LinkedList as the backend. I've picked the ArrayList and created my own implementation of it, including local implementations of the Collection interface. Now I'm struggling with the basic concept of how to adapt the properties of a Bag using the my List class, specifically inserting a List within a List.
The way my List is set up follows the usual Java type, except all the methods aren't implemented, which is why I wrote my own Collection interface type as
public interface Collection<E>
implementing add, remove, size, clear, contains, isEmpty and toArray methods.
The List type is declared as
public class List<E> implements Collection<E>
with three private fields:
private int size;
private E[] data;
private int growSize=10;
the E[] data
holds whatever element type is needed, and growSize is the number of new slots to increase the list size as needed. This works great when the List is used traditionally for single objects, but I get errors when I try to insert a List type.
I'm trying to implement this method:
public boolean add(Collection<E> c){
if (size < data.length){
data= (E[]) new Object[10];
data[0]= c;
}
}
When I try this, it says there's a type mismatch between Collection and E.
Two questions:
- First, what rules of Java generics am I breaking here and why?
- Second, what is needed to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能将参数化类型用于两种不同的类型。在您的情况下(如果我明白您的意思),您尝试将参数化类型
E
用于类型E
本身和类型Collection< /代码>。
如果您尝试在
List
中使用这两种不同的类型,那么实际上泛型没有任何用处。你可以只声明你的List(和超类型Collection)而不需要它。如果您尝试接受 E 类型的对象列表,并将它们逐一添加到您的
List
中,以便每个这样的E 类型对象
将成为列表中的一个元素,那么您应该将实现更改为:You can't use a parameterized-type for two different types. In your case (if I get you), you're trying to use the parameterized-type
E
for both typeE
itself and typeCollection<E>
.If you're trying to have these two different types in your
List<E>
, you actually have no use in Generics. You can just declare your List (and the supertype Collection) without it.If you're trying to accept a list of objects of type E, and add each of them one-by-one to your
List<E>
,so that each such object of typeE
will become an element in your List, then you should change the implementation to something like: