[英]Generic Set/Array Initialization - Java
I am trying to fulfill this interface 我正在尝试实现此界面
Set<T> filter(UnaryRelation<T> x)
I need to create a set containing elements in a set that satisfy x. 我需要创建一个集合,其中包含满足x的元素。 I can't seem to figure out how to initialize the set being that it is said to be generic.
我似乎无法弄清楚如何初始化集合,因为据说它是通用的。 How can I create it so it is not generic?
如何创建它而不是通用的?
Set<T> filteredArray = new Object<T>[size()];
You need to decide what T
is when you invoke that code. 调用该代码时,需要确定
T
是什么。 For example: 例如:
class Test<T> {
private Set<T> filter;
public Test() {
filter = new HashSet<T>();
}
public Set<T> getFilter() {
return filter;
}
}
class Main {
public static void main(String[] args) {
Test<Integer> test = new Test<Integer>();
Set<Integer> filter = test.getFilter();
}
}
https://docs.oracle.com/javase/tutorial/java/generics/types.html https://docs.oracle.com/javase/tutorial/java/generics/types.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.