Java Java Data Structures Efficiency! Implement Chooser UI

Jiawei Hu
Jiawei Hu
9,740 Points

Question regarding byArtist method.

In the case of artistSongs == null, we do:

artistSongs = new ArrayList<>();

(now artistSongs is just an empty ArrayList that has No songs in it), and then we put both the artist and this empty ArrayList of this element into the byArtist() map and then we go for the next element of the mSongs SongBook.

The matter is that we never really manage to put this song into the Map or the artistSongs ArrayList and all we did it just create a map element with key equals the author of this song and an empty ArrayList as value.

Isn't it NOT what we want? Isn't there a line of code missing where it actually adds the song to the newly created artistSongs ArrayList?

1 Answer

andren
andren
28,360 Points

You seem to have missed a line of code that was added. At 6:12 Craig adds the following line right below the if statement:

artistSongs.add(song);

Which adds the song to the list. This line gets executed both when the if statement is hit and when it is not. So the song is always added to the list.