Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在循环中创建新对象?_Java_Arrays_Object - Fatal编程技术网

Java 如何在循环中创建新对象?

Java 如何在循环中创建新对象?,java,arrays,object,Java,Arrays,Object,首先,我是java新手,所以请发发发慈悲吧。 我创建了一个电影类和一个电影类。 MovieList类基本上是一个数组,应该包含电影对象 public class MovieList{ //fields private Movie[] movieList = new Movie[100]; 现在,在MovieList类中,我有一个方法addmovies,我应该在main中调用它。现在我的问题是,如何在for循环中创建应该添加到MovieList的新电影对象。类似于创建从控制台获取其字段的未定义数

首先,我是java新手,所以请发发发慈悲吧。 我创建了一个电影类和一个电影类。 MovieList类基本上是一个数组,应该包含电影对象

public class MovieList{
//fields
private Movie[] movieList = new Movie[100];
现在,在MovieList类中,我有一个方法addmovies,我应该在main中调用它。现在我的问题是,如何在for循环中创建应该添加到MovieList的新电影对象。类似于创建从控制台获取其字段的未定义数量的电影对象。 我试过电影1=新电影();但在for循环中,它被覆盖,我的电影列表最终只包含我从控制台输入的最后一部电影。 我还尝试在main中创建一个新数组,该数组将包含空的电影对象,并且我将在for循环中设置它们的字段,但是该数组也是有限的,我似乎无法增加它。 还有什么我可以拍的电影吗电影2=新电影()。。。以此类推,但对“movie2”使用一个变量,我可以通过某种方式将其更改为movie3

public static void shortMovie(Movie[] arrayMovie,int x,int y){
  Scanner console = new Scanner(System.in);
  String title;
  int id;
  System.out.println("Now please enter the movies, just name and ID");
        for(int i=x; i<y; i++){
           System.out.println("Please enter the detail of the movie in this order");
           System.out.println("Please enter the title of the movie");
           title = console.next();
           System.out.println("Please enter the id of the movie");
           id = console.nextInt();
           arrayMovie[i].setTitle(title);
           arrayMovie[i].setId(id);
           }
publicstaticvoid短片(电影[]arrayMovie,intx,inty){
扫描仪控制台=新扫描仪(System.in);
字符串标题;
int-id;
System.out.println(“现在请输入电影名称和ID”);

对于(inti=x;i首先,如果我正确理解您的问题,您将需要一个集合对象,最好是一个动态增加大小的集合对象

然后,您可以创建一个循环来添加看起来像这样的电影

            int index=0;       
            List<Movie> movieList = new LinkedList<Movie>();
            System.out.println("Enter the movie info ID and Title");
            while (index++ < size) {
                Movie movie = new Movie();
                System.out.println("Movie Title:");
                movie.setTitle(console.next());
                System.out.println("Movie Id:");
                movie.setId(console.nextInt());
                movieList.add(movie);
            }
            System.out.println(movieList.size()); // Count of movie added
int索引=0;
List movieList=new LinkedList();
System.out.println(“输入电影信息ID和标题”);
while(索引+++

希望这能解决您的问题。

您应该先制作电影

在“短片”方法中,尝试:

arrayMovie[i] = new Movie();
arrayMovie[i].setTitle(title);
arrayMovie[i].setId(id);
...

您可能希望使用ArrayList(或其他动态集合)而不是数组:

List<Movie> movieList = new ArrayList<Movie>();

是的,问题是数组有限,但数组列表应该可以解决问题。很高兴我能提供帮助。我应该补充一点,您访问
列表中的项目与常规数组不同:
movieList.get(I)
for(int i=x; i<y; i++){
    System.out.println("Please enter the detail of the movie in this order");
    System.out.println("Please enter the title of the movie");
    title = console.next();
    System.out.println("Please enter the id of the movie");
    id = console.nextInt();
    Movie movie = new Movie();
    movie.setTitle(title);
    movie.setId(id);
    movieList.add(movie);
}
arrayMovie[i] = new Movie();
arrayMovie[i].setTitle(title);
arrayMovie[i].setId(id);