user2219097
user2219097

Reputation: 357

Adding a bunch of items to ArrayList correctly

I have a major problem with how I am adding items to my list. In the for loop I have constructed, I am not sure how to continually add elements to the list. I keep adding only one element which overwrites the last element in the list. So if I was to add 2 elements to my list, after the loop has finished, my array list size will be 1. I'm not sure how to add items properly. I know exactly where the problem lies but am struggling to find any clever way of adding correctly. Will really appreciate any help

tasklist = db.getAllTasks();
    locationlist = db.getAllLocations();

    for (int i = 0; i < locationlist.size(); i++) {
    task_location_list = db.getAllTasksbyLocation(locationlist.get(i)
                .getAddress());


        for (Task task : task_location_list) {
            task_location_list.add(task);

        }
    }

Upvotes: 0

Views: 90

Answers (1)

vipul mittal
vipul mittal

Reputation: 17401

try:

 task_location_list =new ArrayList<Task>();
 for (int i = 0; i < locationlist.size(); i++) {
    task_location_list .addAll(db.getAllTasksbyLocation(locationlist.get(i)
                .getAddress()));

    }

Upvotes: 2

Related Questions