java - 通过循环数组列表来填充 setter

标签 java user-interface arraylist

我正在做一个大学项目。阻止我几乎所有进展的主要问题之一是用数组列表内容填充 setter 。 arraylist 是对象类型。 这是代码:

import java.util.ArrayList;
import java.util.List;

public class Whisky {

    private String name;
    private String description;
    private int quantity;


    public Whisky(String nm, String desc, int quant) {
        this.name = nm;
        this.description = desc;
        this.quantity = quant;


        List<Object> WhiskyList = new ArrayList<Object>() ;
        WhiskyList.add("Balvenie");
        WhiskyList.add("triple cask 12.  Matured in the three most traditional types of cask – first-fill bourbon, refill bourbon and sherry");
        WhiskyList.add(10);
        WhiskyList.add("Glenfiddich");
        WhiskyList.add("Reserve Cask. This Whisky has been matured exclusively in Spanish sherry casks");
        WhiskyList.add(10);
        WhiskyList.add("Laphroig");
        WhiskyList.add("Laphroig Four Oak. Matured in four different types of cask: ex-bourbon barrels, small quarter casks, virgin American oak barrels and European oak hogsheads.");
        WhiskyList.add(10);

   }   


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getQuantity() {
        return quantity;
    }

     public void setQuantity(int quantity) {
        this.quantity= quant;
    }
}

所以基本上我想做的是运行一个 for 循环来遍历 arraylist.size() ,并且对于它所采取的每个“步骤”,它将内容放入正确的 setter 中。那可能吗?

最佳答案

我认为您正在尝试设置威士忌实例的初始列表。有很多方法可以做到这一点。下面是一个使用静态列表并使用静态方法填充它的方法(在类加载时运行一次。)

public class Whisky {

  public static final List<Whisky> WhiskyList = new ArrayList<Whisky>();

  static {
    WhiskyList.add(new Whisky("Balvenie",
                              "triple cask 12.  Matured in the three most traditional types of cask – first-fill bourbon, refill bourbon and sherry",
                              10));
    WhiskyList.add(new Whisky("Glenfiddich",
                              "Reserve Cask. This Whisky has been matured exclusively in Spanish sherry casks",
                              10));
    WhiskyList.add(new Whisky("Laphroig",
                              "Laphroig Four Oak. Matured in four different types of cask: ex-bourbon barrels, small quarter casks, virgin American oak barrels and European oak hogsheads.",
                              10));
  }

  private String name;
  private String description;
  private int quantity;

  public Whisky(String nm, String desc, int quant) {
    this.name = nm;
    this.description = desc;
    this.quantity = quant;
  }

  // finish with getters and settings
}

关于java - 通过循环数组列表来填充 setter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58755204/

相关文章:

java - 使用 JAVA REGEX 搜索任何给定字符串

java - 为什么 OpenJPA 集群自动生成 id?

java - 循环内的ArrayList

java - ArrayList.size() 方法是否被缓存?

java - 创建 JToolBar 时出错

Java:在字段或构造函数中初始化 ArrayList?

java - 创建仅打印到一行的 XML

java - JDialog 未在父 JFrame 上居中

c++ - 如何为命令行程序制作图形用户界面?

html - 像 JotForm.com 中的面板栏?