我在处理这段代码时遇到了问题。基本上,给出了主要功能,并要求开发类 CountDown
的最简单版本,用于编译代码。
类主要:
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
public static void main(String[] args) {
CountDown a = new CountDown(3,15);
CountDown b = new CountDown(2,20);
CountDown c = new CountDown(3,15);
List<CountDown> lst = new ArrayList<CountDown>();
lst.add(a);
lst.add(b);
lst.add(c);
Set<CountDown> set = new HashSet<CountDown>();
set.addAll(lst);
lst.clear();
lst.addAll(set);
Collections.sort(lst);
for(E e : lst) {
System.out.println(e);
}
}
}
倒计时类:
public class CountDown implements Comparable<CountDown> {
private int hour;
private int minute;
public CountDown(int hour, int minute) throws Exception {
if ((hour > 23 || hour < 0) && (minute > 59 || minute < 0)) {
throw new IllegalArgumentException("Horas ou minutos invalidos");
} else {
this.hour = hour;
this.minute = minute;
}
}
public int gethour() {
return this.hour;
}
public int getminute() {
return this.minute;
}
@Override
public int compareTo(CountDown arg0) {
int result = 0;
int minute1 = arg0.getminute();
int hour1 = arg0.gethour();
result = this.getminute() - minute1;
if(result == 0) {
result = this.gethour() - hour1;
}
return result;
}
}
我的问题是在 Main 函数中这段代码无法编译,我不知道如何让它工作。有人可以教我怎么了吗?
for(E e : lst) {
System.out.println(e);
}
最佳答案
您的 lst
变量是 CountDown
对象的列表,因此如果您在此处将 E
更改为 CountDown
:
for(E e : lst) {
它应该可以工作。
关于java - 对于具有通用类型的迭代器 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48271409/