thegrimbeeper
thegrimbeeper

Reputation: 11

Merging 2 arraylists into one

I am trying to combine 2 arraylists into 1. I am receiving incompatible types error when i compile. I have no clue where my mistake is. Any help would be greatly appreciated.

ArrayList<Course> myCourse = new ArrayList<Course>();
        myCourse.add(coursesTaken);
        myCourse.add(currentSemesterCourses);

Upvotes: 1

Views: 119

Answers (2)

Braj
Braj

Reputation: 46871

use ArrayList#addAll() that appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.

myCourse.addAll(coursesTaken);

instead of

myCourse.add(coursesTaken);

I hope coursesTaken is a Collection that contains item of type Course or any subclass of Course.

Upvotes: 2

Elliott Frisch
Elliott Frisch

Reputation: 201527

You want to use ArrayList.addAll(Collection), and you could use the ArrayList(Collection) constructor as well -

ArrayList<Course> myCourse = new ArrayList<Course>(coursesTaken);
myCourse.addAll(currentSemesterCourses);

Upvotes: 0

Related Questions