简体   繁体   English

将另一个类中的元素添加到数组列表中

[英]Add an element from another class to an array list

Our teacher gave us a new task and somehow I am not able to figure out the problem. 我们的老师给了我们一项新任务,以某种方式我无法解决问题。

There are 3 different java classes and I am only allowed to change the ToDoList class. 有3个不同的Java类,只允许更改ToDoList类。 There, I want to add a new List so that the main class is able to add new Items to my todo list. 在那里,我想添加一个新列表,以便主类能够将新项目添加到我的待办事项列表中。 As you can see below, I tried to initialize a new list but that did not work. 正如您在下面看到的那样,我尝试初始化一个新列表,但这没有用。

Where is my mistake? 我的错误在哪里?

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

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

Then comes the next where I tried to add an array but which did not work: 接下来是下一个我尝试添加数组但无法正常工作的地方:

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;
}
}

And in the end, the main class should supposed to create a new ToDo List and print it out (Note that I did not include the print() Method): 最后,主类应该创建一个新的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();

When you add a new entry to the list, you don't set the next pointer of that entry. 将新条目添加到列表时,无需设置该条目的下一个指针。 But in the print() method, you use the next pointer, but (if you don't set it somewhere else) it is still null. 但是在print()方法中,您使用了下一个指针,但是(如果未在其他地方设置)它仍然为null。 Try your add() method like this: 尝试这样的add()方法:

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

But are you sure you can use an ArrayList here? 但是您确定可以在此处使用ArrayList吗? I get the impression that you have to implement a linked list. 我觉得您必须实现一个链表。 In this case the code would look like this: 在这种情况下,代码如下所示:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM