简体   繁体   中英

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. 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):

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. Try your add() method like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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