Reputation: 11
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
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
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