【问题标题】:Add an element from another class to an array list将另一个类中的元素添加到数组列表中
【发布时间】:2016-03-18 23:12:59
【问题描述】:

我们的老师给了我们一个新任务,但不知何故我无法找出问题所在。

有 3 个不同的 java 类,我只能更改 ToDoList 类。在那里,我想添加一个新列表,以便主类能够将新项目添加到我的待办事项列表中。正如您在下面看到的,我尝试初始化一个新列表,但没有奏效。

我的错误在哪里?

public class ToDoListEntry {
   String task;
   LocalDate date;
   ToDoListEntry next;

   public ToDoListEntry(LocalDate date, String task) {
      this.task = task;
      this.date = date;
   }
}

接下来是我尝试添加一个数组但不起作用的地方:

public class ToDoList {
   ToDoListEntry first;

   public ArrayList<ToDoListEntry> todolist;

   public ToDoList (){
      todolist = new ArrayList<ToDoListEntry>();
   }

   public void add(ToDoListEntry newTask) {
      todolist.add(newTask);
  }

  public String print() {
    String result = "";
    if (first == null) {
        result = "Empty list!\n";
    } else {
        ToDoListEntry pointer = first;
        while (pointer != null) {
            result += "Until " + pointer.date + " Task: "
                    + pointer.task +"\n";
            pointer = pointer.next;
        }
    }
    System.out.println(result);
    return result;
}
}

最后,主类应该创建一个新的 ToDo List 并打印出来(注意我没有包含 print() 方法):

public static void main(String[] args) {

    System.out.println("Test 00: Empty List");
    ToDoList list2016 = new ToDoList();

    list2016.print(); 

    System.out.println("Test 01: add");
    list2016.add(new ToDoListEntry(LocalDate.of(2016, 8, 15), "Do workout"));
    list2016.add(new ToDoListEntry(LocalDate.of(2016, 6, 3), "Buy apples"));
    list2016.add(new ToDoListEntry(LocalDate.of(2016, 10, 11), "Read Books"));
    list2016.print();

【问题讨论】:

  • 你导入了吗?请发布错误。
  • 您可以将您提到的错误添加到您的帖子中吗?
  • 定义“不起作用”。恰恰。您收到的错误信息是什么?另外,鉴于 ToDoListEntry 有一个名为 next 的 ToDoListEntry 类型的字段,我认为您不应该使用 ArrayList,而是将条目相互链接(即了解链表的原理)。跨度>
  • 好的,很抱歉将其称为错误!到目前为止,我没有收到任何错误,但问题是所有三个待办事项都没有添加到我的待办事项列表中。为此,我不明白为什么不能添加这些。
  • @Tom 你说得对 ;)

标签: java arrays class linked-list


【解决方案1】:

当您向列表中添加新条目时,您不会设置该条目的下一个指针。但是在 print() 方法中,您使用 next 指针,但是(如果您没有在其他地方设置它)它仍然为空。试试你的 add() 方法:

public void add(ToDoListEntry newTask) {
    todolist.add(newTask);
    if (todolist.size() >= 2) todolist.get(todolist.size()-2).next = newTask;
}

但是你确定你可以在这里使用 ArrayList 吗?我的印象是你必须实现一个链表。在这种情况下,代码如下所示:

class ToDoListEntry {
    String task;
    LocalDate date;
    ToDoListEntry next;

    public ToDoListEntry(LocalDate date, String task) {
       this.task = task;
       this.date = date;
    }
}

public class ToDoList {
    ToDoListEntry first;
    int size;

    public ToDoList (){
        first = null;
        size = 0;
    }

    public void add(ToDoListEntry newTask) {
        if (first == null){
            first = newTask;
        }else{
            ToDoListEntry pointer = first;
            while (pointer.next != null){
                pointer = pointer.next;
            }
            pointer.next = newTask;
        }
        size++;
    }

    public String print() {
        String result = "";
        if (first == null) {
            result = "Empty list!\n";
        } else {
            ToDoListEntry pointer = first;
            while (pointer != null) {
                result += "Until " + pointer.date + " Task: " + pointer.task +"\n";
                pointer = pointer.next;
            }
        }
        System.out.println(result);
        return result;
    }
}

【讨论】:

  • 这真的很有帮助!感谢那!现在我只剩下一个问题:如何按日期对列表条目进行排序?所以第一个条目是下一个到期的待办事项?
  • @Yanlu 你可以通过调用Collections.sort( list ); 并实现Comparable&lt;TodoListEntry&gt; 来做到这一点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-13
  • 1970-01-01
  • 1970-01-01
  • 2011-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多