我正在尝试这些问题:https://www.codeeval.com/browse/214/以下是我的代码和输出: 的 CODE
public class TimeSort implements Comparable<TimeSort>{
public int hour;
public int minutes;
public int seconds;
public TimeSort(int hour, int minutes, int seconds) {
this.hour = hour;
this.minutes = minutes;
this.seconds = seconds;
}
public TimeSort(String str){
/*DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
final String x = str;
try {
Date time = sdf.parse(x);
//Time time = new Time(new SimpleDateFormat("HH:mm:ss").parse(x).getTime());
this.hour = time.getHours();
this.minutes = time.getMinutes();
this.seconds = time.getSeconds();
} catch (ParseException e) {
e.printStackTrace();
}*/
String[] parts = str.split(":");
int hour = Integer.parseInt(parts[0]);
int minutes = Integer.parseInt(parts[1]);
int seconds = Integer.parseInt(parts[2]);
this.hour = hour;
this.minutes = minutes;
this.seconds = seconds;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getMinutes() {
return minutes;
}
public void setMinutes(int minutes) {
this.minutes = minutes;
}
public int getSeconds() {
return seconds;
}
public void setSeconds(int seconds) {
this.seconds = seconds;
}
@Override
public String toString() {
if(this.getHour() < 10){
return "0"+this.getHour()+":"+this.getSeconds()+":"+this.getSeconds();
}
return this.getHour()+":"+this.getSeconds()+":"+this.getSeconds();
}
public static void main(String[] args){
List<String> inputs = new ArrayList<>();
inputs.add("02:26:31 14:44:45 09:53:27");
inputs.add("05:33:44 21:25:41");
inputs.add("02:26:31 14:44:45 09:53:27 02:26:31 01:44:45 19:53:27");
for (String input : inputs){
sortTimes(input);
}
}
@Override
public int compareTo(TimeSort timeSort) {
if(this.getHour() > timeSort.getHour()){
return -1;
}
else if(this.getHour() < timeSort.getHour()){
return 1;
}
else if(this.getHour() == timeSort.getHour()) {
if(this.getMinutes() > timeSort.getMinutes()){
return -1;
}
else if(this.getMinutes() < timeSort.getMinutes()){
return 1;
}
}
else if(this.getSeconds() > timeSort.getSeconds()){
return 1;
}
return 0;
}
public static void sortTimes(String str){
List<TimeSort> list = new ArrayList<>();
String[] times = str.split(" ");
for (String time : times){
list.add(new TimeSort(time));
}
System.out.print("Before Sorting: ");
for (TimeSort t : list){
System.out.print(t + " ");
}
System.out.println();
Collections.sort(list);
System.out.print("After Sorting: ");
for (TimeSort t : list){
System.out.print(t + " ");
}
System.out.println();
System.out.println("==============================================================");
}
}
输出
Before Sorting: 02:31:31 14:45:45 09:27:27
After Sorting: 14:45:45 09:27:27 02:31:31
==============================================================
Before Sorting: 05:44:44 21:41:41
After Sorting: 21:41:41 05:44:44
==============================================================
Before Sorting: 02:31:31 14:45:45 09:27:27 02:31:31 01:45:45 19:27:27
After Sorting: 19:27:27 14:45:45 09:27:27 02:31:31 02:31:31 01:45:45
==============================================================
我看到的奇怪的事情是时间不能正确打印。例如02:26:31打印为02:31:31。即使我试图解析字符串时间(代码的注释部分),它也是一样的
感谢任何帮助。感谢
答案 0 :(得分:3)
您的toString()
方法存在错误:
@Override
public String toString() {
if(this.getHour() < 10){
return "0"+this.getHour()+":"+this.getSeconds()+":"+this.getSeconds();
}
return this.getHour()+":"+this.getSeconds()+":"+this.getSeconds();
}
请注意,第二个字段是getSeconds()
而不是getMinutes()
。
答案 1 :(得分:1)
您的toString()
- 方法无法打印会议记录!
答案 2 :(得分:0)
其他答案在您的代码中发现了错误。
仅供参考,所有这些代码都是不必要的。 Java 8及更高版本内置了java.time框架。其中的类是LocalTime
,用于表示没有日期且没有时区的仅限时间的值。此类已经知道如何compare并对其值进行排序以及如何parse / print其值。默认情况下,打印遵循标准ISO 8601格式。
List<LocalTime> times = new ArrayList<>();
times.add( LocalTime.parse( "02:26:31" );
times.add( LocalTime.parse( "14:44:45" );
times.add( LocalTime.parse( "09:53:27" );
Collections.sort( times );
转储到控制台。
System.out.println( times );