Home > Blockchain >  Shifting Successive Indexes Up After Removing Element
Shifting Successive Indexes Up After Removing Element

Time:12-13

I am able to run this in order to delete an element from an array, but how can I shift the movies up so it doesn't display as -1, 0, 1, etc... This is a screenshot of output: https://imgur.com/a/Ab0PP3p

 public static String[] removeMovies (String[] newList) { 
    String[] removeMovies = new String [newList.length-1]; 
    
    for (int i=0; i < newList.length-1; i  ) { 
        removeMovies[i]= newList [i 1]; 
    }

    Scanner scan= new Scanner(System.in); 
    System.out.println("Which movie would you like to delete?");
    removeMovies[removeMovies.length-1]=scan.nextLine(); 
    return removeMovies;
    }
public static void dltMovieList(String[] movies) {

    for (int i=0; i<movies.length-1;i  ) { 
    System.out.println((i-1) ")"  movies[i]);
}

CodePudding user response:

You just want to check you entered movie is matched with movie inside array if matched then skip the loop. Change i < movies.length - 1 to i < movies.length because loop can not print out last value.

Here down is my code:

public static String[] removeMovies (String[] arr) { 
    String[] newArr = new String [arr.length-1]; 
    Scanner scan= new Scanner(System.in); 
    System.out.println("Which movie would you like to delete?");
    String movieDel = scan.nextLine();
    for(int x = 0, k = 0; x < arr.length; x  ){
        if(!arr[x].equals(movieDel)){
            newArr[k] = arr[x];
            k  ;
        }
    }
    return newArr;
}
    
public static void printList(String[] arr) {
    for (int i = 0; i < arr.length; i  ) { 
         System.out.println((i 1) ") "  arr[i]);
    }
}
    
public static void main(String[] args) {
    String[] movies = {"Money Heist","Games Of Thrones","Under The Dome","Stranger Things","Dark"};
    movies = removeMovies(movies);
    printList(movies);
}

Your Expected Output:

Which movie would you like to delete?
Under The Dome
1) Money Heist
2) Games Of Thrones
3) Stranger Things
4) Dark

CodePudding user response:

Here is my implementation of the problem:

import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    String[] movies = {"The Avengers","Rush Hour","Fast & Furious 7","The Ugly Truth","Spiderman"};
    dltMovieList(removeMovies(movies));
  }
  public static String[] removeMovies (String[] newList) { 
    String[] removeMovies = new String [newList.length-1]; 
    Scanner scan= new Scanner(System.in); 
    System.out.println("Which movie would you like to delete?");
    String movieDel = scan.nextLine();
    for(int x = 0; x < newList.length; x  ){
      if(!newList[x].equals(movieDel)){
        removeMovies[x] = newList[x];
      }
    }
    return removeMovies;
  }
  public static void dltMovieList(String[] movies) {

    for (int i=0; i<movies.length;i  ) { 
      System.out.println((i 1) ") "  movies[i]);
    }
  }
}

Running this code will output:

Which movie would you like to delete?
Spiderman
1) The Avengers
2) Rush Hour
3) Fast & Furious 7
4) The Ugly Truth

How this code works is that, similar to yours, we create a new array to store the movie list after the deletion of a movie.

Then, we take user input to find out what movie they want to delete.

When iterating through our array to copy over the values, if we run into that value, we skip it and don't copy it over.

Finally, when we print out the values, we want to start at i = 0 and go up until i = movies.length - 1 so that we get all of the values. Additionally, when print out the index, we should add 1 so that we start at 1.

I hope this helped! Let me know if you have any questions!

  • Related