Silly question regarding the island project

At the 8th part of this project, it asks to swap songs so i wrote a simple code, however, it looks a bit messy for some reason

Is there a better way to swap two values of an ArrayList?

https://www.codecademy.com/courses/learn-java/projects/java-desert-island-playlist

   //I've add the ArrayList items just for context.

    poirotsToDos.add("visit the crime scene");
    poirotsToDos.add("interview suspects");
    poirotsToDos.add("let the little grey cells do their work");
    poirotsToDos.add("trim mustache");
    poirotsToDos.add("call all suspects together");
    poirotsToDos.add("reveal the truth of the crime");

    //messy code:

    int toDoOne = poirotsToDos.indexOf("visit the crime scene");
    int toDoTwo = poirotsToDos.indexOf("trim mustache");
    String auxNdx = poirotsToDos.get(toDoOne);

    poirotsToDos.set(toDoOne, poirotsToDos.get(toDoTwo));
    poirotsToDos.set(toDoTwo ,auxNdx);

this code works just fine but i wonder if is any ez way to do it.

Thanks!

1 Like

Hey there - welcome to the forum!

The way you’ve done it is the way that most people approach the challenge of swapping two variables - swap the two things around by way of an intermediate third variable.

There is, however, a built-in method that you can use to swap two items in a List.

import java.util.ArrayList;
import java.util.Collections;

class Main {
  public static void main(String[] args) {
    System.out.println("Example of using 'swap()' on an ArrayList object.");

    ArrayList<String> poirotsToDos = new ArrayList<String>();

    poirotsToDos.add("visit the crime scene");
    poirotsToDos.add("interview suspects");
    poirotsToDos.add("let the little grey cells do their work");
    poirotsToDos.add("trim mustache");
    poirotsToDos.add("call all suspects together");
    poirotsToDos.add("reveal the truth of the crime");

    System.out.println("Existing ArrayList:");
    System.out.println(poirotsToDos);

    int first_swap = poirotsToDos.indexOf("visit the crime scene");
    int second_swap = poirotsToDos.indexOf("trim mustache");

    Collections.swap(poirotsToDos, first_swap, second_swap);

    System.out.println("Swapped ArrayList:");
    System.out.println(poirotsToDos);
  }
}

The output from the console is:

Example of using 'swap()' on an ArrayList object.
Existing ArrayList:
[visit the crime scene, interview suspects, let the little grey cells do their work, trim mustache, call all suspects together, reveal the truth of the crime]
Swapped ArrayList:
[trim mustache, interview suspects, let the little grey cells do their work, visit the crimescene, call all suspects together, reveal the truth of the crime]

Whether or not Java is doing it the same way as you behind the scenes, who knows. :wink: At least, by using the swap() method, you can save yourself a bit of typing. :slight_smile:

I’ve always found it suspect to search for specific things in a list. It can be the right thing to do but it’s usually not.

How about replacing all occurrences of those into the other one? Could use map to replace a with b, b with a, and others are kept.

import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

class Main {
    public static void main(String[] args) {
        List<String> poirotsToDos = Arrays.asList
            ( "visit the crime scene"
            , "interview suspects"
            , "let the little grey cells do their work"
            , "trim mustache"
            , "call all suspects together"
            , "reveal the truth of the crime"
            );

        String a = "visit the crime scene";
        String b = "trim mustache";

        List<String> result =
            poirotsToDos
            .stream()
            .map(s -> s.equals(a) ? b
                    : s.equals(b) ? a
                    : s)
            .collect(Collectors.toList());

        System.out.println("Existing ArrayList:\n" + poirotsToDos);
        System.out.println("After swapping:\n" + result);
    }
}
2 Likes

:astonished:

I didn’t know we could declare an ArrayList like this!

Would it be that way I’ve declared the exact same thing as that way you did?

1 Like

Yeah. I knew about the .swap but I wasnt sure i was supposed to use it there. But yeah, it is way better with .swap!

Thanks!

1 Like