import java.util.*;public class Schedule { // Map 用于存储课程安排信息 private Map<String, List<String>> schedule; public Schedule() { this.schedule = new HashMap<>(); } // 添加课程信息 public void addSchedule(String time, String course, String classroom) { if (schedule.containsKey(time)) { List<String> courses = schedule.get(time); if (courses.contains(course) || courses.contains(classroom)) { System.out.println("Error: Course or classroom already scheduled at this time."); } else { courses.add(course); courses.add(classroom); schedule.put(time, courses); System.out.println("Schedule added successfully."); } } else { List<String> courses = new ArrayList<>(); courses.add(course); courses.add(classroom); schedule.put(time, courses); System.out.println("Schedule added successfully."); } } // 获取课程安排信息 public List<String> getSchedule(String time) { if (schedule.containsKey(time)) { return schedule.get(time); } else { return null; } } public static void main(String[] args) { Schedule schedule = new Schedule(); schedule.addSchedule("Monday 9:00", "Math", "Room 101"); schedule.addSchedule("Monday 9:00", "Physics", "Room 102"); schedule.addSchedule("Monday 10:00", "English", "Room 103"); List<String> monday_9 = schedule.getSchedule("Monday 9:00"); System.out.println("Monday 9:00 schedule: " + monday_9.toString()); }}请将每一句代码解释一下
时间: 2024-02-01 15:03:20 浏览: 92
java代码-import java.util.Scanner;
这是一个Java程序,名为Schedule,用于存储和获取课程安排信息。下面是代码的解释:
```java
import java.util.*; // 导入Java工具包
public class Schedule { // 定义一个名为Schedule的类
// Map 用于存储课程安排信息
private Map<String, List<String>> schedule;
// 类的构造函数,用于初始化schedule
public Schedule() {
this.schedule = new HashMap<>();
}
// 添加课程信息
public void addSchedule(String time, String course, String classroom) {
if (schedule.containsKey(time)) { // 判断时间是否已经被占用
List<String> courses = schedule.get(time); // 获取该时间段已经占用的课程信息
if (courses.contains(course) || courses.contains(classroom)) {
// 判断该课程或教室是否已经被占用
System.out.println("Error: Course or classroom already scheduled at this time.");
} else {
courses.add(course);
courses.add(classroom);
schedule.put(time, courses); // 将课程信息添加到schedule中
System.out.println("Schedule added successfully.");
}
} else {
List<String> courses = new ArrayList<>();
courses.add(course);
courses.add(classroom);
schedule.put(time, courses); // 将课程信息添加到schedule中
System.out.println("Schedule added successfully.");
}
}
// 获取课程安排信息
public List<String> getSchedule(String time) {
if (schedule.containsKey(time)) {
return schedule.get(time); // 返回该时间段的课程信息
} else {
return null; // 如果该时间段没有课程信息,则返回null
}
}
// 主函数
public static void main(String[] args) {
Schedule schedule = new Schedule(); // 创建一个Schedule对象
schedule.addSchedule("Monday 9:00", "Math", "Room 101"); // 添加课程信息
schedule.addSchedule("Monday 9:00", "Physics", "Room 102");
schedule.addSchedule("Monday 10:00", "English", "Room 103");
List<String> monday_9 = schedule.getSchedule("Monday 9:00"); // 获取课程信息
System.out.println("Monday 9:00 schedule: " + monday_9.toString()); // 打印课程信息
}
}
```
阅读全文