Arrays and ArrayLists final project

Even with the video tutorial, I got stuck with the steps 7 and over on this exercise (you need Pro to get into this page).
https://www.codecademy.com/paths/introduction-to-android-with-java/tracks/java-arrays-and-loops/modules/learn-java-arrays-and-arraylists/projects/java-desert-island-playlist

The problem is: I was able to remove one song so that my playlist only has 5 songs, but I can’t fire all the subsequent steps correctly.

Steps 8-10 are all about swapping song a to song b and the song b is supposed to appear twice in the list; despite many tries my list still displays both songs a and b, instead of showing doubled song b.

import java.util.ArrayList;

class Playlist {
  
  public static void main(String[] args) {
    ArrayList<String> desertIslandPlaylist = new
    ArrayList<String>();

    desertIslandPlaylist.add("Ich bin nackt");
    desertIslandPlaylist.add("Younger Now");
    desertIslandPlaylist.add("99 Luftballoons");
    desertIslandPlaylist.add("Night on the Galactic Railroad");
    desertIslandPlaylist.add("My Heart Will Go On");
    desertIslandPlaylist.add("Despacito");

  System.out.println(desertIslandPlaylist);
  
  desertIslandPlaylist.remove("Younger Now");
  
  System.out.println(desertIslandPlaylist.size());

  int indexA = desertIslandPlaylist.indexOf("Despacito");
     int indexB = desertIslandPlaylist.indexOf("My Heart Will Go On");

  String tempA = "Despacito";

  desertIslandPlaylist.set(indexA, "My Heart Will Go On");

desertIslandPlaylist.remove("Younger Now");

System.out.println(desertIslandPlaylist.size());

  }
  
}
 java Playlist
[Ich bin nackt, Younger Now, 99 Luftballoons, Night on the Galactic Railroad, My Heart Will Go On, Despacito]
5
$ java Playlist
[Ich bin nackt, Younger Now, 99 Luftballoons, Night on the Galactic Railroad, My Heart Will Go On, Despacito]
5
$ java Playlist
[Ich bin nackt, Younger Now, 99 Luftballoons, Night on the Galactic Railroad, My Heart Will Go On, Despacito]
5

I think my code has some issues but I can’t identify them on my own. Any insights much appreciated.

What happens when you print out the actual array after modifying it?

It worked. The key was to printing the Array after each modification :slight_smile: Thank you so much for the tip.

1 Like