Add an element from another class to an array list

Yanlu

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();
Feanor

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jquery remove and add class in list element

From Dev

How to add items into a ListView from a List of another class

From Dev

AngularJS Directive to add class on click, but remove and add it to another element if clicked

From Dev

Check if list array contains element in another list

From Dev

Thread safe remove/add element from one list to another

From Dev

jQuery: Dynamically add class that belongs to another element

From Dev

How to add object from a class to List in another class.

From Dev

How to remove a class in a list of elements and add to another element Jquery?

From Dev

add Class to element by id from within directive

From Dev

how to add class to element from its id?

From Dev

Maximum of element from List based on the another one

From Dev

Get an element from arrayList inside another class

From Dev

Input element from one list to add to another

From Dev

Add class if element is followed by another

From Dev

Groovy: Add some element of list to another list

From Dev

PHP eliminate array element based on selected element from another array

From Dev

AngularJS Directive to add class on click, but remove and add it to another element if clicked

From Dev

python add objects from list into another list

From Dev

print the kth element from the list. list will be obtained by based on number present in another array

From Dev

How can I remove a list from another list using a field value of the element's type if i can't modify the class directly?

From Dev

How to add an element to Arraylist from another activity?

From Dev

How to add a class to an element when another element gets a class in angular?

From Dev

Maximum of element from List based on the another one

From Dev

Add class to an element, only when another class is added

From Dev

How to add a class to element if another element is empty?

From Dev

How to add element to a list, inside another list?

From Dev

If element has class add class to another element

From Dev

Vuejs add css class to ul li list from array

From Dev

Add multiple class objects from another class

Related Related

  1. 1

    jquery remove and add class in list element

  2. 2

    How to add items into a ListView from a List of another class

  3. 3

    AngularJS Directive to add class on click, but remove and add it to another element if clicked

  4. 4

    Check if list array contains element in another list

  5. 5

    Thread safe remove/add element from one list to another

  6. 6

    jQuery: Dynamically add class that belongs to another element

  7. 7

    How to add object from a class to List in another class.

  8. 8

    How to remove a class in a list of elements and add to another element Jquery?

  9. 9

    add Class to element by id from within directive

  10. 10

    how to add class to element from its id?

  11. 11

    Maximum of element from List based on the another one

  12. 12

    Get an element from arrayList inside another class

  13. 13

    Input element from one list to add to another

  14. 14

    Add class if element is followed by another

  15. 15

    Groovy: Add some element of list to another list

  16. 16

    PHP eliminate array element based on selected element from another array

  17. 17

    AngularJS Directive to add class on click, but remove and add it to another element if clicked

  18. 18

    python add objects from list into another list

  19. 19

    print the kth element from the list. list will be obtained by based on number present in another array

  20. 20

    How can I remove a list from another list using a field value of the element's type if i can't modify the class directly?

  21. 21

    How to add an element to Arraylist from another activity?

  22. 22

    How to add a class to an element when another element gets a class in angular?

  23. 23

    Maximum of element from List based on the another one

  24. 24

    Add class to an element, only when another class is added

  25. 25

    How to add a class to element if another element is empty?

  26. 26

    How to add element to a list, inside another list?

  27. 27

    If element has class add class to another element

  28. 28

    Vuejs add css class to ul li list from array

  29. 29

    Add multiple class objects from another class

HotTag

Archive