java 创建一个 ArrayList 并添加项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5033926/
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
Creating an ArrayList and adding items
提问by Will
Im learning Java and having a problem with ArrayList.
我正在学习 Java 并且在使用 ArrayList 时遇到了问题。
Firstly I have a class called Item, with which I create various item objects. Then I have a class Catalogue which is an array list and should hold a list of the item objects I create. At the moment I can manually add the items to the catalogue by invoking an addItem method on the Catalogue object and manually entering the name of the item object I want to add (item1 item2 item3 etc) But I wanted to know if there is a way to add the items to the ArrayList automatically each time I create an item object?
首先,我有一个名为 Item 的类,我用它创建了各种项目对象。然后我有一个类 Catalog,它是一个数组列表,应该保存我创建的项目对象的列表。目前,我可以通过在 Catalog 对象上调用 addItem 方法并手动输入我要添加的项目对象的名称(item1 item2 item3 等)来手动将项目添加到目录中,但我想知道是否有办法每次创建项目对象时自动将项目添加到 ArrayList 中?
I should mention, my list needs to hold an infinite amount of items, so I have not specified a size in my code. Any help would be greatly appreciated :) Thanks
我应该提到,我的列表需要容纳无限数量的项目,所以我没有在我的代码中指定大小。任何帮助将不胜感激:) 谢谢
import java.util.ArrayList;
public class Catalogue
{
private ArrayList<Item> catalogue;
public Catalogue ()
{
catalogue = new ArrayList<Item>();
}
public void addAnItem(Item item)
{
catalogue.add(item);
}
}
回答by Matten
Use the Catalogue
as an Item
factory:
使用Catalogue
作为Item
工厂:
public class Catalogue
{
...
public Item createItem()
{
Item item = new Item();
catalogue.add(item);
return item;
}
...
}
Another approach: Make Catalogue
singleton and let the items add themselves.
另一种方法:制作Catalogue
单例并让项目自行添加。
回答by Codemwnci
One way you could do this, is if you passed the Catalogue into the constructor of the Item class, and once the item is set up, add the item to the catalogue at that point.
您可以这样做的一种方法是,如果您将 Catalog 传递给 Item 类的构造函数,并且一旦设置了该项目,就在该点将该项目添加到目录中。
It may look something like this
它可能看起来像这样
public Item(Catalogue catalogue) {
// set up item here
// finally add item to the catalogue
catalogue.addAnItem(this);
}
回答by extraneon
I have put some comments at Matten and Codemwnci's answers, and here is an explanation of them.
我对 Matten 和 Codemwnci 的回答发表了一些评论,这里是对它们的解释。
Codemwnci suggests that you should not be able to construct an Item without setting its catalogue.
Codemwnci 建议您不应该在不设置其目录的情况下构建项目。
public class Item {
public Item(Catalog catalog) {
// set up item here
// finally add item to the catalog
catalog.addAnItem(this);
}
}
This explicit constructor removes the implicit default (no-arg) constructor, and you cannot construct an Item without it having a valid, non-null catalog.
此显式构造函数删除了隐式默认(无参数)构造函数,并且您不能在没有有效的非空目录的情况下构造 Item。
If you have various types of items, with (slightly) different behaviour, you might be better served with Matten's answer (although slightly changed here).
如果您有各种类型的物品,行为(略有)不同,那么 Matten 的回答可能会更好地为您服务(尽管这里略有改动)。
As an example I'm using a Book (which is your Item). My Book has a title, author, textAtTheBack, and weight.
作为一个例子,我正在使用一本书(这是你的项目)。我的书有标题、作者、textAtTheBack 和权重。
interface Book {
String getTitle();
String getAuthor();
String getTextAtTheBack();
Long getWeight(); // in grams, can be very heavy!
}
public class Catalog {
private ArrayList<Book> catalogue;
public Book createPaperback(final String title, final String author,
final String tatb, final Long weight) {
Book b = new Book() {
String getTitle() { return title; }
String getAuthor() {return author; }
String getTextAtTheBack() {return tatb;}
Long getWeight() {return weight;}
}
catalogue.add(b);
return b;
}
public Book createEBook(final String title, final String author,
final String tatb) {
Book b = new Book() {
String getTitle() { return title; }
String getAuthor() {return author; }
String getTextAtTheBack() {return tatb;}
Long getWeight() {return 0;} // Yep - no weight!
}
catalogue.add(b);
return b;
}
}
Alternatively, you could have different catalogues:
或者,您可以有不同的目录:
public abstract class Catalogue {
private final List<Book> books = new ArrayList<Book>;
public abstract Book (final String title, final String author,
final String tatb, final Long weight);
/** Find the book with the given title (not null) in the current catalogue.
* @return the book, or null if not found.
*/
public void findBook(String title) {
for (Book b : books) {
if (b.getTitle().equalsIgnoreCase(title)) {
return b;
}
}
return null;
}
protected void addBookToCatalogue(Book b) {
books.add(b);
}
}
public class EbookCatalogue extends Catalogue {
public Book (final String title, final String author,
final String tatb, final Long weight) {
Book b = new Book() {
String getTitle() { return title; }
String getAuthor() {return author; }
String getTextAtTheBack() {return tatb;}
Long getWeight() {return 0;} // ignore weight
}
addBookToCatalogue(b);
return b;
}
}
In the rest of the program you can have multiple catalogues, each with a slightly different type of Book, but the program need not know that.
在程序的其余部分中,您可以有多个目录,每个目录的 Book 类型略有不同,但程序不需要知道这一点。
I think in this case the simple Constructor of codemwnci is best, but there alternative solutions if your situation warrants a more flexible solution.
我认为在这种情况下 codemwnci 的简单构造函数是最好的,但如果您的情况需要更灵活的解决方案,还有其他解决方案。