【问题标题】:Replacing original array with edited array in Java用Java中的编辑数组替换原始数组
【发布时间】:2022-01-17 09:33:50
【问题描述】:

一旦数组被编辑(添加或删除电影)并且用户返回主菜单再次列出电影,编辑后的数组不会输出。有没有办法在每次操作完成时替换数组以便打印出一个新数组?输出:https://imgur.com/Etm1uXB

*我们不允许在这个作业中使用 ArrayLists

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(); 
}

【问题讨论】:

  • 您可能希望为此使用 ArrayList。
  • 没有“原始数组”。您在每次通话时都会创建一个新的。您可能希望将其存储在字段中。

标签: java arrays for-loop


【解决方案1】:

我同意 MAK 成员的观点,就像他在评论中所说的那样,您的代码的理想解决方案是用 ArrayList 实例或 Vector 实例(基本相同)替换数组“电影”,以便您可以添加或删除像你一样的元素。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

对于使用静态方法的用例,最好的解决方案是拥有一个不在方法之间传递的电影列表。这样,列表始终是最新的,不需要替换,事情也不会消失。当我们这样做时,如果您使用列表List&lt;String&gt; movies 而不是数组String[] movies,您的代码将更易于管理和使用。我在下面添加了 cmets 来解释它是如何工作的:

//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(); 
}

要打印当前和最新的电影列表,您可以调用更新后的listMovies(); 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 2013-06-08
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多