【问题标题】:Scheduled Calendar in JavaJava 中的计划日历
【发布时间】:2017-01-25 11:04:15
【问题描述】:

有人能解释一下这个框架代码中输入的来源吗?

骨架代码如下:

数据.java

import java.util.Date;
import java.util.List;

public interface Data {

// Return a list of all appointments at the given location (at any time)
// If there are no such appointments, return an empty list
// Throws IllegalArgumentException if the argument is null
public List<Schedule> getSchedule(String location);

// Return the next appointment at or after the given time (in any location)
// If there is no such appointment, return null
// Throws IllegalArgumentException if the argument is null
public Schedule getNextSchedule(Date when);

// Return the next appointment at or after the given time, at that location
// If there is no such appointment, return null
// Throws IllegalArgumentException if any argument is null
public Schedule getNextSchedule(Date when, String location);

// Create a new appointment in the calendar
// Throws IllegalArgumentException if any argument is null
public void add(String description, Date when, String location);

// Remove an appointment from the calendar
// Throws IllegalArgumentException if the argument is null
public void remove(Schedule schedule);
}

日历.java

import java.util.Date;
import java.util.List;

public class Calendar implements Data {

// We will use this when we test
public Calendar() {
}

@Override
public List<Schedule> getSchedule(String location) {
    // TODO
    return null;
}

@Override
public Schedule getNextSchedule(Date when) {
    if(when == null) {
        throw new IllegalArgumentException("time was null");
    }
    // TODO
    return null;
}

@Override
public Schedule getNextSchedule(Date when, String location) {
    // TODO
    return null;
}

@Override
public void add(String description, Date when, String location) {
    // TODO
}

@Override
public void remove(Schedule schedule) {
    // TODO
}
}

Schedule.java

import java.util.Date;

public interface Schedule {

public String getDescription();

public String getLocation();

public Date getStartTime();

}

我也想知道:

  • 从哪里开始,我试图开始,但我不确定在第一个标记为 getSchedule 的待办事项部分中返回什么。我知道我无法返回位置,因为该方法要求返回 List 类型(?)。

【问题讨论】:

  • 我认为回答具体和明确定义的问题会更容易。考虑提出具体问题,而不是“解释这段长代码的作用”。
  • 点了,我在底部添加了一些问题,我会继续添加更多,因为我会继续尝试代码。
  • 阅读有关列表/集合的文档。
  • 按照自己的方式学习在线 Java 教程可能比让这里的人教你更好。
  • 我认为您不需要添加更多内容。我认为您最好删除并更改为具体的简短问题。

标签: java interface calendar hashtree


【解决方案1】:

用于 Collections 和 Maps 等类中。意味着有一个集合(例如一个 ArrayList)可以包含 Schedule 类型的元素。如果你有一个像 HashMap 这样的 Map,那么你需要在这些 中放置 2 种类型:Key(例如 Integer)和 Object(例如 String)

HashMap<Integer, String>

现在您可以使用从地图中获取元素

thisIsMyMap.get(7);

此方法返回类型计划的实例,其中键为 7,存储在整数中。您必须使用 Integer 而不是 int,因为 Map 或 List 不能包含 int、double、boolean 等简单类型。

现在解决您的问题: 在您可以在 getSchedule(String location) 方法中返回某些内容之前,您需要有一个 List 来存储您的 Schedules,如下所示:

ArrayList<Schedule> schedules = new ArrayList<Schedule>();

然后您可以在方法 getSchedule(String location) 中迭代列表并将所有具有正确位置的条目写入另一个列表中并返回它:

@Override
public List<Schedule> getSchedule(String location) {
    ArrayList<Schedule> right = new ArrayList<Schedule>();
    for(int i = 0; i<schedules.size(); i++)
    {
        if(schedules.get(i).getLocation().equals(location))
            right.add(schedules);
    }
    return right;
}

您写道您想使用 HashTrees。我认为如果你只使用 ArrayList 会更好更快。是的,您可以按特定顺序放置 HashTree,但它要复杂得多,而且它也适用于 List。

反对数

【讨论】:

  • 我尝试使用该代码,但尽管 schedules arraylist 具有通用类型的调度,但它一直让我强制转换(调度),当我进行转换时,arraylist 被视为空数组......我该如何解决?我在想有人让时间表数组列表包含输入,但我不明白该怎么做
  • 你在哪里投什么?如果您没有在其中放置任何值,则 arraylist 是一个空数组。尝试制作一个添加和删除时间表的界面。
猜你喜欢
  • 1970-01-01
  • 2023-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 2015-12-12
  • 1970-01-01
相关资源
最近更新 更多