我正在尝试用Java编写一个Set<E>
接口,该接口将由另一个类mySet<E>
使用arrayList<E>
来存储元素。
我打算包含常规设置功能:add()
,remove()
,union()
intersection()
等。
我的add()
和remove()
函数的类型应该是什么?我尝试过使用add(Object E)
和add(<E>)
,但遇到了错误。
答案 0 :(得分:2)
add(E objToAdd);
remove(E objToRemove);
答案 1 :(得分:0)
public class SystemDemo {
public static void main(String[] args) {
// this will list the current system properties
Properties p = System.getProperties();
p.list(System.out);
}
}
通常会延伸AbstractSet<K>
并执行:
Set<K>
答案 2 :(得分:0)
每当我们编写泛型类型的实现时我们使用与接口/类相同的类型表示法。
public interface ISet<E>{
boolean add(E element);
boolean remove(E element);
}
根据您的问题,您需要创建此实现,您可以尝试以下示例代码:
public class MySet<E> implements ISet<E>{
private List<E> myInnerList = new ArrayList<E>();
public boolean add(T element){
//write you code here for adding the element
// i.e. myInnerList.add(element);
return false;
}
public boolean remove(T element){
//write you code here for adding the element
// i.e. myInnerList.remove(element);
return false;
}
}
希望这能解决您的问题。