Home > Back-end >  Replacing original array with edited array in Java
Replacing original array with edited array in Java

Time:12-14

Once the array is edited (a movie is added or deleted) and user returns to main menu to list movies again, the edited array is not outputted. Is there a way to replace the array every time an action is completed so it prints out a new array? output:https://imgur.com/Etm1uXB

*we are not allowed to use ArrayLists for this assignment

public static void listMovies() { 
    String[]movies = {"This is Us","Ghostbusters","Shrek","Interstellar","Pacific Rim"};
    
    for(int i=0; i<movies.length; i  ) {
        System.out.println((i 1) ") " movies[i]);
    }
    returnToMenu();
}
private static void addMovies(String[]movies) { 
    reenterUser(); 
    String[]moreMovies = new String[movies.length 1];

    for(int i=0; i<movies.length; i  ) {
        moreMovies[i]=movies[i];
    }
    
    Scanner input = new Scanner(System.in);
    
    System.out.println("Add a movie: ");
    moreMovies[moreMovies.length-1]=input.nextLine();
    System.out.println("You have added a movie!");
    
    System.out.println("This is an updated list of movies available at the rental store: ");
    
        printMovies(moreMovies); 
        returnToMenu(); 
}

CodePudding user response:

I Agree with the member MAK and like he said in the comment the ideal solution for your code is to replace the array "movies" with an ArrayList instance or a Vector intance ( its basically the same ) so you can add or remove elements as you which.

CodePudding user response:

For your use case with static methods, the best solution is to have a single list of movies that are not passed around between methods. This way the list is always be up to date and does not need to be replaced and things will not disappear. While we are at it, your code will be a lot more manageable and easy to work with if you use a list List<String> movies instead of an array String[] movies. I have added comments below to explain more on how it works:

//Move the movie list outside of the method so it becomes a class variable that every method can use.
//It also needs to be static to match your methods.
//Change the type to a List to allow you to easily add more movies.
public static List<String> movies = new ArrayList<>(Arrays.asList("This is Us", "Ghostbusters", "Shrek", "Interstellar", "Pacific Rim"));

//Call this method whenever you want to print the list
public static void listMovies() {
    //iterate list and print movies, we need to use `movies.size()` to get the length of a list
    for(int i=0; i<movies.size(); i  ) {
        //use the "get(i)" method to get a movie form the list
        System.out.println((i 1) ") " movies.get(i));
    }
    //returnToMenu();
}

//This method does not need an input anymore now that we have a class variable that stores the movies
private static void addMovies() { 
    //I am not sure what this line does
    //reenterUser();

    Scanner input = new Scanner(System.in);
    
    System.out.println("Add a movie: ");
    //Add the new movie to the list using the add method "movies.add(value)"
    movies.add(input.nextLine());
    System.out.println("You have added a movie!");
    
    System.out.println("This is an updated list of movies available at the rental store: ");
    
    //Print out the updated movie list:
    listMovies();

    //Return to the menu
    returnToMenu(); 
}

To print the current and up to date list of movies you can call the updated listMovies(); method.

  • Related