一旦达到限制就展开数组

Expand array once it reaches limit

我有一个程序,我需要在一个数组中打印出几个 Movie object(仅由一个字符串表示)。我从一个文本文件开始,其中已经有五部电影,但在控制台中,如果 he/she 需要,我将允许用户扩展数组。 (我不能在这个问题中使用数组列表)。我试图设计一个执行此操作的程序,但每次尝试添加新电影时都会出现越界异常。我还需要检查数组,看看里面是否有重复的电影 object?我怎样才能做到这一点?

问题:如何允许用户扩展数组并向列表中添加更多电影?我如何检查数组以查看其中是否已经有某个电影标题?

public class MovieDriver {

    //variable declaration
    static Movie[] movies = new Movie[5];
    static Scanner scan = new Scanner(System.in);
    static int input = 0;
    static String title = "";

    public static void main(String[] args) throws FileNotFoundException {

        //retrieves movie data
        getData();

        System.out.println("Welcome to the favorite movie program.");

    do{ 
        System.out.println("Press 1 to print the list, 2 to add another movie, 3 to end the program.");

        input = scan.nextInt();     

        switch(input) {

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

            break;

        case 2:
            System.out.println("Please enter the movie you would like to add to the list:");
            title = scan.nextLine();
            movies = Arrays.copyOf(movies, movies.length+1);
            movies[movies.length] = new Movie(title);

            break;

        case 3:
            System.out.println("Program terminated.");

            break;



            }
        } while (input != 3);
    }

    // method to retrieve data
    public static void getData() throws FileNotFoundException {
        // reads in movie data
        File MovieData = new File("./src/Movies.txt");
        Scanner fileScanner = new Scanner(MovieData);
        int i = 0;

        // while there is a new line in the data, goes to the next one
        while (fileScanner.hasNextLine()) {
            String line = fileScanner.nextLine();
            Scanner lineScanner = new Scanner(line);
            String title = lineScanner.nextLine();

            // creates a movie
            movies[i] = new Movie(title);
            i++;
        }
    }
}

数组扩大1:

Movie[] temp = new Movie[movies.length + 1];
System.arraycopy(movies, 0, temp, 0, movies.length);
movies = temp;

要检查是否有重复的电影对象,请在您的 Movie class 中实施 equals。然后遍历现有的电影,并检查是否 movies[i].equals(newMovie).

要扩展数组,您必须重新创建一个所需大小的数组,并将前一个数组的内容复制到新数组中。

   static Movie[] movies = new Movie[5];

  // expanding
  Movie[] newMovieArray = new Movie[10];
  for(int i = 0; i < 5; i++){
       newMovieArray[i] = movies[i];
  }
  movies = newMovieArray;

第一个问题:IndexOutOfBound

不需要使用System.arraycopy()。 您使用 Arrays.copyOf() 的正确方法。问题来自

movies = Arrays.copyOf(movies, movies.length+1); 
movies[movies.length] = new Movie(title);  // <--- here

调用 copyOf() 后,数组的新长度为 6。当您调用 movies[movies.length]=... 时,您尝试访问 7th 元素。只是做:

movies = Arrays.copyOf(movies, movies.length+1); 
movies[movies.length-1] = new Movie(title);  // it will set the last slot of the array

但是 Scanner 还有第二个问题。当您使用 nextInt() 扫描输入时,不会读取 end-of-line。这意味着如果您输入 2(添加电影),将读取 2,而不是 new-line。然后 new-line 被 title = scan.nextLine() 读取并且你有一个空标题...... 解决方案是:

case 2:
   scan.nextLine();  // <-- add this to "eat" the previous new-line
   System.out.println("Please enter the movie you would like to add to the list:");
   title = scan.nextLine();
   movies = Arrays.copyOf(movies, movies.length+1);
   movies[movies.length-1] = new Movie(title);
   break;

第二题:检查重复项

由于您只有一个 plain/native 数组并且电影之间没有排序,您可以实现一个简单的 for 循环,例如:

case 2:
   scan.nextLine();  // <-- add this to "eat" the previous new-line
   System.out.println("Please enter the movie you would like to add to the list:");
   title = scan.nextLine();
   if (!checkDuplicate(title)) {
      movies = Arrays.copyOf(movies, movies.length+1);
      movies[movies.length-1] = new Movie(title);
   }
   break;

并添加一个函数(假设有一个Movie#getTitle():

    private static boolean checkDuplicate(String title) {
        for (Movie m : movies) {
            if (title.equals(m.getTitle())) {
                return true;
            }
        }
        return false;
    }