1

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); 
1
  • 1
    When you're posting a question regarding an error, please include (copy and paste) the actual error message and stack trace in your question. Also, make sure you post all relevant code - the error is about incompatible types, but you don't include the declarations of coursesTaken and currentSemesterCourses, so we can't see what their types are. Commented Jun 13, 2014 at 19:21

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.