Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.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 创建ArrayList并添加项_Java_Arraylist - Fatal编程技术网

Java 创建ArrayList并添加项

Java 创建ArrayList并添加项,java,arraylist,Java,Arraylist,我正在学习Java,但ArrayList有问题 首先,我有一个名为Item的类,我用它创建各种Item对象。 然后我有一个类目录,它是一个数组列表,应该包含我创建的项目对象列表。 目前,我可以通过调用Catalog对象上的addItem方法并手动输入我要添加的item对象的名称(item1 item2 item3等),将项目手动添加到目录中 但我想知道是否有一种方法可以在每次创建item对象时自动将这些项添加到ArrayList中 我应该提到,我的列表需要容纳无限多的项目,所以我没有在代码中指定

我正在学习Java,但ArrayList有问题

首先,我有一个名为Item的类,我用它创建各种Item对象。 然后我有一个类目录,它是一个数组列表,应该包含我创建的项目对象列表。 目前,我可以通过调用Catalog对象上的addItem方法并手动输入我要添加的item对象的名称(item1 item2 item3等),将项目手动添加到目录中 但我想知道是否有一种方法可以在每次创建item对象时自动将这些项添加到ArrayList中

我应该提到,我的列表需要容纳无限多的项目,所以我没有在代码中指定大小。 任何帮助都将不胜感激:) 谢谢

import java.util.ArrayList;
公开课目录
{
私人ArrayList目录;
公共目录()
{ 
目录=新的ArrayList();
}
公共无效添加项(项目)
{
目录.增加(项目);
}
}

目录
用作
项目
工厂:

public class Catalogue
{
  ...

  public Item createItem()
  {
    Item item = new Item();
    catalogue.add(item);
    return item;
  }

  ...
}

另一种方法:创建
目录
单例,让项目自行添加。

可以这样做的一种方法是,将目录传递到项目类的构造函数中,一旦项目设置完毕,就将项目添加到目录中

它可能看起来像这样

public Item(Catalogue catalogue) {
   // set up item here

   // finally add item to the catalogue
   catalogue.addAnItem(this);
}

我在Matten和Codemwnci的回答中发表了一些评论,下面是对它们的解释

Codemwnci建议您在不设置商品目录的情况下不能构建商品

public class Item {
 public Item(Catalog catalog) {
   // set up item here

   // finally add item to the catalog
   catalog.addAnItem(this);
 }
}
此显式构造函数删除隐式默认(无参数)构造函数,如果项没有有效的非空目录,则无法构造该项

如果您有不同类型的物品,行为(稍微)不同,那么您可能会更好地使用Matten的答案(尽管此处略有变化)

作为一个例子,我正在使用一本书(这是你的物品)。我的书有书名、作者、书背和重量

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;
  }
}
接口书{
字符串getTitle();
字符串getAuthor();
字符串getTextAtTheBack();
Long getWeight();//以克为单位,可能非常重!
}
公共类目录{
私人ArrayList目录;
公共图书纸质版(最终字符串标题、最终字符串作者、,
最终管柱tatb,最终长重){
书b=新书(){
字符串getTitle(){return title;}
字符串getAuthor(){return author;}
字符串getTextAtTheBack(){return tatb;}
Long getWeight(){返回权重;}
}
目录.添加(b);
返回b;
}
公共图书CreateBook(最终字符串标题、最终字符串作者、,
最终字符串(tatb){
书b=新书(){
字符串getTitle(){return title;}
字符串getAuthor(){return author;}
字符串getTextAtTheBack(){return tatb;}
Long getWeight(){return 0;}//是-没有权重!
}
目录.添加(b);
返回b;
}
}
或者,您可以有不同的目录:

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;
    }
}
公共抽象类目录{
私有最终列表书籍=新ArrayList;
公开摘要书(最终字符串标题、最终字符串作者、,
最终管柱tatb,最终长重);
/**在当前目录中查找具有给定标题(非空)的书。
*@归还书籍,如果找不到,则为空。
*/
公共作废findBook(字符串标题){
(b册:书籍){
if(b.getTitle().equalsIgnoreCase(标题)){
返回b;
}
}
返回null;
}
受保护的无效addBookToCatalogue(b册){
增加(b);
}
}
公共类电子书目录扩展目录{
公共书籍(最终字符串标题、最终字符串作者、,
最终管柱tatb,最终长重){
书b=新书(){
字符串getTitle(){return title;}
字符串getAuthor(){return author;}
字符串getTextAtTheBack(){return tatb;}
Long getWeight(){return 0;}//忽略权重
}
将账簿添加到目录(b);
返回b;
}
}
在程序的其余部分,您可以有多个目录,每个目录都有稍微不同的图书类型,但程序不需要知道这一点


我认为在这种情况下,codemwnci的简单构造函数是最好的,但如果您的情况需要更灵活的解决方案,则有其他解决方案。

这就完成了任务,如果我当天没有投票的话,将得到+1。然而,值得注意的是,这将您限制在一个目录中。Singleton通常被认为是一种反模式。@glowcoder——对于SingleCatalog,您的意思是实现Singleton模式,是吗?使用工厂模式允许多个目录(以及多个目录中的项目),因此这是我的第一个想法:)为了清晰:这是目录中的一种方法。如果不调用目录的createItem(),则不能创建项目。可能是通过保护Item构造函数包或将Item定义为接口,并让createItem实例化Item的匿名内部类实现。如果进展太快(在这个阶段对威尔来说),还不用麻烦:)非常好,extraneon。为了清晰起见,我编辑了我的帖子。。。如果多个开发人员在项目中工作,使用受保护的构造函数并不能防止包中的滥用,因此接口+匿名实现是最安全的方法。带有私有构造函数的内部类是另一种方法。@Matten我添加了一个自己的答案,无耻地窃取了两个主要答案:)这也是一个非常好的构造。如果所有项目都是相同的(属于同一类),则可以说比Matten的工厂模式更好(如果有不同类型的项目则更好)。。。您不应该从中删除
摘要
关键字吗
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;
    }
}