[英]For iterator with generic type - Java
我在使用这段代码时遇到了麻烦。 基本上,给出了主要功能,并且要求开发编译代码的最简单版本的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);
}
}
}
类CountDown :
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) {
它应该工作。
为了使其正常工作,您可能需要一些东西。
首先,您需要一个简单的界面或E
类:
public interface E
{
}
将以下位添加到CountDown
。 注意事情发生变化的评论:
// Added "implements E" and provided trivial interface E
// to allow (for E : ...) to work in main(...).
public class CountDown implements E, Comparable<CountDown> {
private int hour;
private int minute;
// Removed unnecessary "throws Exception" specifier
public CountDown(int hour, int minute) {
// Previous logic incorrect. Should throw exception if either hour
// OR minute is out of range.
if (hour > 23 || hour < 0 || minute > 59 || minute < 0) {
throw new IllegalArgumentException("Horas ou minutos invalidos");
} else {
this.hour = hour;
this.minute = minute;
}
}
// Corrected capitalisation to make bean compliant name.
// Not strictly required.
public int getHour() {
return this.hour;
}
// Corrected capitalisation to make bean compliant name.
// Not strictly required.
public int getMinute() {
return this.minute;
}
@Override
public int compareTo(CountDown other) {
// Simplified logic. Made sort by hours, then by minutes.
int cmp = Integer.compare(this.getHour(), other.getHour());
if (cmp == 0)
cmp = Integer.compare(this.getMinute(), other.getMinute());
return cmp;
}
// Really should have equals(...) method if instances are comparable.
// Also required if we want to use instances in a HashSet
@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (o instanceof CountDown)
{
CountDown other = (CountDown)o;
// Ensures that this logic is consistent with compareTo(...)
return this.compareTo(other) == 0;
}
return false;
}
// Really should have hashCode() if we have equals.
// Also required if we want to use instances in a HashSet
@Override
public int hashCode()
{
// Collision-free hash code given logic in constructor.
return this.hour * 60 + this.minute;
}
// Required to show a sensible value for System.out.print(...) etc
@Override
public String toString()
{
return String.format("%s[%02d:%02d]", getClass().getSimpleName(),
this.getHour(), this.getMinute());
}
}
鉴于这些变化, main(...)
应该无需修改即可运行。
要删除编译错误,您需要做两件事:
lst
包含CountDown
类型的元素,因此增强的for循环变量应该是CountDown
类型而不是E
即for(CountDown e : lst) { ... }
。 public static void main(String[] args) throws Exception {
或将代码包装在try
/ catch
。 如果你需要显示CountDown类的值,请覆盖tostring()方法..希望它能工作......
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Test{
public static void main(String[] args) throws Exception {
List<CountDown> lst = new ArrayList<CountDown>();
CountDown a = new CountDown(3,15);
CountDown b = new CountDown(2,20);
CountDown c = new CountDown(3,15);
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(CountDown e : lst) {
System.out.println(e);
}
}
}
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;
}
}
它有两个错误。 1. CountDown类构造函数标记为“抛出异常”。 当您创建它的实例时,需要将其包装到Try-Catch块中。 2.迭代CountDown列表的集合时,for循环中指定的类型是错误的。 3.修复System.out.println以打印用户友好的信息。
public class Main {
public static void main(String[] args) {
List<CountDown> lst = new ArrayList<CountDown>();
try
{
CountDown a = new CountDown(3,15);
CountDown b = new CountDown(2,20);
CountDown c = new CountDown(3,15);
lst.add(a);
lst.add(b);
lst.add(c);
}
catch (Exception ex)
{
}
Set<CountDown> set = new HashSet<CountDown>();
set.addAll(lst);
lst.clear();
lst.addAll(set);
Collections.sort(lst);
for(CountDown e : lst) {
System.out.println("Hours: " + e.gethour() + " Minutes: " + e.getminute());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.