java 将类的实例添加到java中的Arraylist

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2674867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:18:52  来源:igfitidea点击:

Adding instances of a class to an Arraylist in java

javaarraylist

提问by Olga

I have 10 instances of the class movie which I wish to add to an Arraylist named Catalogue1

我有 10 个类电影的实例,我希望将它们添加到名为 Catalogue1 的 Arraylist

in a class containing a main method I write the following

在包含 main 方法的类中,我写了以下内容

 ArrayList catalogue1= new ArrayList ()

   //the class movie is defined in another class
    Movie movie1= new Movie ()
   Movie movie2= new Movie ()

 Catalogue.Add (1, movie1)

What is wrong? Should I define somewhere what kind of Objects this arraylist named catalogue should contain?

怎么了?我应该在某处定义这个名为目录的数组列表应该包含什么样的对象吗?

Thank you in advance

先感谢您

回答by Carl

List<Movie> catalogue = new ArrayList<Movie>(10);
catalogue.add(movie1);
catalogue.add(movie2);
...
catalogue.add(movie10);

is how you would do it with generics.

是您将如何使用泛型做到这一点。

Also note, that generally the practice is to use the interface (List) instead of the concrete implementation (ArrayList) when declaring, so that later if you decide that a LinkedListor perhaps some special MovieListis preferable for the actual implementation then you have less to fix.

另请注意,通常的做法是在声明时使用接口 ( List) 而不是具体实现 ( ArrayList) ,以便以后如果您决定 aLinkedList或某些特殊MovieList实现更适合实际实现,那么您需要解决的问题就更少了。

Oh-HO:

哦-何:

Also, your addas written won't work - it should throw an IndexOutOfBoundsException. The special List version of addrequires that a position already exists (or is 0) before you can add there. So if you want to use that add:

此外,您add所写的将不起作用 - 它应该抛出一个IndexOutOfBoundsException. add的特殊List 版本要求位置已经存在(或为 0),然后您才能在那里添加。因此,如果您想使用该添加:

catalogue.add(0,movie10);
catalogue.add(0,movie9);
...
catalogue.add(0,movie1);

if you want them in 1-10 order.

如果您希望它们按 1-10 个顺序排列。

回答by M. Jessup

If that is your verbatim code there are two immediate problems. First your ArrayList variable is "catalogue1" but you are attempting to add an item to "Catalogue", which doesn't exist. Secondly you are adding an item at position 1, which would be out of bounds, since the arraylist is empty. So I would change the code (adding generics as mentioned by Carl's comment:

如果这是您的逐字代码,则有两个直接问题。首先,您的 ArrayList 变量是“catalogue1”,但您正尝试向“目录”中添加一个不存在的项目。其次,您要在位置 1 添加一个项目,这将超出范围,因为 arraylist 为空。所以我会更改代码(添加 Carl 评论中提到的泛型:

ArrayList<Movie> catalogue1 = new ArrayList<Movie>();
Movie movie1= new Movie();
Movie movie2= new Movie();
catalogue1.add(movie1); //equivalently: catalogue.add(0, movie1);
catalogue1.add(movie2);

回答by Tendayi Mawushe

There are some simple typos in your code:

您的代码中有一些简单的错别字:

ArrayList catalogue1 = new ArrayList();
Movie movie1= new Movie();
Movie movie2= new Movie();
// Catalogue.Add (1, movie1)
catalogue1.add(movie1);
catalogue1.add(movie2);

As you can see from the code above the two problems you had are:

从上面的代码可以看出,您遇到的两个问题是:

  1. The add()method of ArrayListstarts with a lower-case aand you used an upper-case A. Java identifiers are case sensitive.
  2. You were calling the add()method on an object named: Cataloguebut you named your ArrayList: catalogue1.
  1. ArrayListadd()方法以小写开头,您使用了大写. Java 标识符区分大小写。aA
  2. add()在名为的对象上调用该方法:Catalogue但您将ArrayList命名为: catalogue1

回答by Stefan Kendall

There are several problems here. First of all, if you need to add at the beginning of the list, you need to add at index 0, not 1. Furthermore, if you have JVM 1.5+, you should be using generics.

这里有几个问题。首先,如果您需要在列表的开头添加,则需要在索引 0 处添加,而不是 1。此外,如果您有 JVM 1.5+,则应该使用泛型。

List catalogue = new ArrayList<Movie>();
..
...
catalogue.add(movie1);
catalogue.add(movie2);//or (0,movie2)