一旦达到限制就展开数组

时间:2015-03-15 19:02:42

标签: java arrays eclipse file variables

我有一个程序,我需要在数组中打印出几个Movie对象(仅由一个String表示)。我从一个已经有五部电影的文本文件开始,但在控制台中,我允许用户扩展数组,如果他/她想要的话。 (我不能在这个问题上使用arraylist)。我试图设计一个执行此操作的程序,但每次尝试添加新电影时我都会遇到一个超出范围的异常。我还需要检查数组,看看里面是否有一个重复的电影对象?我怎样才能做到这一点?

问题:如何让用户扩展阵列并将更多电影添加到列表中?如何检查数组以查看其中是否已存在某个电影标题?

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++;
        }
    }
}

3 个答案:

答案 0 :(得分:2)

第一个问题:IndexOutOfBound

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

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

致电copyOf()后,数组的新长度为6.当您执行movies[movies.length]=...时,您会尝试访问第7个元素。只是做:

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()扫描输入时,不会读取行尾。这意味着如果输入2(添加电影),则会读取2,但不会读取新行。然后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;

第二个问题:检查重复

由于你只有一个普通/本机数组而且电影之间没有排序,你可以实现一个简单的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;
    }

答案 1 :(得分:1)

将数组扩展为1:

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

要检查是否存在重复的电影对象,请在equals课程中实施Movie。然后迭代现有的电影,并检查是否movies[i].equals(newMovie)

答案 2 :(得分:0)

要展开数组,必须重新创建所需大小的数组,并将以前数组的内容复制到新数组。

   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;