szu*_*ufi 2 java compareto treeset
我做了一个名为"People"的类,它有一个String名称.现在我想使用TreeSet比较两个对象.
public class People<T> implements Comparable<T> {
public TreeSet<People> treeSet;
public String name;
public People(String name)
{
treeSet = new TreeSet();
this.name = name;
}
Run Code Online (Sandbox Code Playgroud)
.....
@Override
public int compareTo(T y) {
if(this.name.equals(y.name)) blablabla; //Here I get error
}
Run Code Online (Sandbox Code Playgroud)
错误:
Cannot find symbol
symbol: variable name;
location: variable y of type T
where T is a type variable
T extends Object declared in class OsobaSet
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这个问题?
Comparable接口中的泛型类型代表将要比较的对象类型.
这是您的示例的正确用法:
public class People implements Comparable<People>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,方法签名将是
@Override
public int compareTo(People y) {
if (this.name.equals(y.name)) { ...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
68 次 |
| 最近记录: |