Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Efficiency! Menu UI

Konstantinos Pedarakis
Konstantinos Pedarakis
21,301 Points

addSong method vs add method of List

hi in this example in the line 47, if i wanted to use the add method of the ArrayList why im getting an error? typing something like that

songBook.add(song);

instead of:

songBook.addSong(song);

it is supposed that SongBook is an ArrayList as we defined in the Contructor of SongBook class, so i can't understand why im getting an error on this. any clarifications would be appreciate.

Thanks

1 Answer

andren
andren
28,558 Points

SongBook is not a list, at no point is it defined as such, in the constructor of SongBook you set mSong (which is a private member variable within SongBook) to be a new ArrayList, but that does not make SongBook itself an ArrayList.

That is why the addSong method is needed in the first place, so that you can add things to the mSong list.

It's no different from having a class with String or int member variables, setting those variables in the Class's constructor would not make the Class a String or an int, the same applies to Lists.

Konstantinos Pedarakis
Konstantinos Pedarakis
21,301 Points

andren Thanks for the clarification. Now i got it 100%. i don't know i was confused so much with that. i shouldn't! anyway thank you very much.