[英]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.