java - 该对象未添加到 ArrayList 中。如何解决这个问题?

标签 java inheritance arraylist

我正在为种植屋制作控制台应用程序。房子有 6 排,每排有 25 个部分。我使用行和节的数组列表构建了这个结构。现在我想根据用户输入将植物添加到该部分。我

我尝试了多种方法来创建不同的方法,您可以在评论中看到这些方法,但没有任何效果,对象植物要么没有创建,要么没有添加到 ArrayList 植物中。

public class Section {
private int number;
private List<TomatoPlant> plants;

public int getNumber() {
    return number;
}

public Section(int number, List<TomatoPlant> plants) {
    this.number = number;
    this.plants = plants;
}

public Section(int number){
   this.number=number;
    //this.addPlants();
   this.plants= new ArrayList<>();
}

public List getPlants() {
    return this.plants;
}

public void addPlants(Cocktailtomatos cocktailtomato) {
    //this.plants= new ArrayList<>();
    this.plants.add(cocktailtomato);
    this.plants.add(Cocktailtomatos.createCocktailtomato("ss", "A", 1));
}

@Override
public String toString() {
    return "Section{" +
            "number=" + number +
            ", plants=" + plants +
            '}';
}

public void addPlant(String id, String row, int sectionnumber) {
    Cocktailtomatos cocktailtomato = new Cocktailtomatos(id, row, sectionnumber, true, false, false, 0, 10);
    this.addPlants(cocktailtomato);
}
}







public class Manager {
private static Manager ourInstance = new Manager();

public static Manager getInstance() {
    return ourInstance;
}

private Manager() {
}

Planthouse planthouse = new Planthouse();

public void addCocktailTomato(){
   Scanner scanner = new Scanner(System.in);

   System.out.println("Input Id:");
   String id = scanner.nextLine();
   System.out.println("Input Row");
   String row = scanner.nextLine();
   System.out.println("Input sectionnumber");
   Integer sectionnumber = scanner.nextInt();
   //Cocktailtomatos cocktailtomato = Cocktailtomatos.createCocktailtomato(id, row, sectionnumber);
   //cocktailtomato.setId(row + sectionnumber + "_" + id);
   //planthouse.getRow(row).getSection(sectionnumber).getPlants().add(cocktailtomato);
   //System.out.println("ID: ");
   //Cocktailtomatos cocktailtomato = new Cocktailtomatos(id, row, sectionnumber, true, false, false, 0, 10);
   //planthouse.getRow(row).getSection(sectionnumber).getPlants().add(cocktailtomato);

   //Cocktailtomatos cocktailtomato = Cocktailtomatos.createCocktailtomato(id, row, sectionnumber);
   //planthouse.getRow(row).getSection(sectionnumber).addPlants(cocktailtomato);

    planthouse.getRow(row).getSection(sectionnumber).addPlant(id, row, sectionnumber);
   //this.getPlantlist();
}

public int getPlantlist() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Input row:");
    String row = scanner.nextLine();
    System.out.println("Input sectionnumber:");
    Integer sectionnumber = scanner.nextInt();
   //return planthouse.getRow(row).getSection(sectionnumber).getPlants();
   //System.out.println(planthouse.getRow(row).getSection(sectionnumber).getPlants().size());
   System.out.println(planthouse.getRows());


       //System.out.println(planthouse.getRow("A").getSections().size());
       //for(int i=0; i<planthouse.getRow("A").getSections().size(); i++){
           //System.out.println(planthouse.getRow("A").getSections().get(i).getPlants());
       //}
       //for(int i=0; i<planthouse.getRows().size(); i++){


           // System.out.println(planthouse.getRows().get(i));
      // }

     return planthouse.getRow(row).getSection(sectionnumber).getPlants().size();
   }

  }





 public class Planthouse {
    private ArrayList<Row> rows = new ArrayList<>();

public Planthouse(){
    this.initRows();

}

public Planthouse(ArrayList<Row> rows) {
    this.setRows(rows);
}

public ArrayList<Row> getRows() {
    return rows;
}

private void initRows() {
    this.rows= new ArrayList<>();
    this.rows.add(new Row("A"));
    this.rows.add(new Row("B"));
    this.rows.add(new Row("C"));
    this.rows.add(new Row("D"));
    this.rows.add(new Row("E"));
    this.rows.add(new Row("F"));




}

public void setRows(ArrayList<Row> rows) {
    this.rows = rows;
}

public void printPlanthouse(){
    for(Row row: rows) {
        System.out.println(row.getId());
        row.printSections();
    }

}


public Row getRow(String rowId) {
    Row row = new Row(rowId);
    return row;

}

    public void addPlant(){

    }

    public Section getSection(Row row, Section section) {
    Planthouse planthouse = new Planthouse();
    planthouse.getRow(row.getId()).getSection(section.getNumber());
    return section;
    }
}

 public class Row {
private String id;
private ArrayList<Section> sections;

public Row(String id) {
    this.id = id;
    this.initSections();

}

public Row(String id, ArrayList<Section> sections) {
    this.id = id;
    this.setSections(sections);

}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public ArrayList<Section> getSections() {
    return sections;
}

public void setSections(ArrayList<Section> sections) {
    this.sections = sections;
}


public void initSections() {
    this.sections = new ArrayList<>();
        for (int i = 1; i <=25; i++) {
           this.sections.add(new Section(i));


        }

   }

    public void printSections() {
        for (Section section : sections) {
            System.out.println(section.getNumber());
        }
    }
    public Row(){

    }

@Override
public String toString() {
    return "Row{" +
            "id='" + id + '\'' +
            ", sections=" + sections +
            '}';
}

public Section getSection(int sectionnumber){

    //Section section = new Section(sectionnumber);
    Section section = sections.get(sectionnumber-1);

    return section;

}
}

方法 getPlantList() 打印出所有行及其部分以及每个部分及其植物,并且在用户将植物添加到部分后,这些部分将被打印为空(没有植物)。所以我得出的结论是,要么工厂没有创建,要么没有添加到数组列表中。有人可以建议我问题出在哪里吗?谢谢。

最佳答案

每次新行未放置在整个数据层次结构中时,当前的 getRow 都会创建。

public Row getRow(String rowId) {
    for (Row row : rows) {
        if (row.getId().equals(rowId)) {
            return row;
        }
    }
    // Either throw new IllegalArgumentException(); or:
    Row row = new Row(rowId);
    rows.add(row);
    return row;
}

相反,或者除了列表之外,您可能希望使用从 rowId 到 Row 的映射。

Map<String, Row> rowMap = new HashMap<>();
Map<String, Row> rowMap = new TreeMap<>(); // Sorted
Map<String, Row> rowMap = new LinkHashMap<>(); // In order of insertion, chronological

String rowId = "A";
rowMap.put(rowId, row);
Row row = rowMap.get(rowId);

关于java - 该对象未添加到 ArrayList 中。如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56159735/

相关文章:

c# - Asp.NET MVC 5 中 Controller 的继承

java - 数组 ArrayList python 等价物

java - getLastNonConfigurationInstance() 的问题 - 似乎只返回 null

Java FutureTask - 多线程调用 get()

c++ - 惰性构造 - 虚拟方法与 if-then stub setter/getter

java - 有没有更有效的方法在ArrayList和Array之间进行转换

c - 如何在数组中保存单词

java - 解决 Apache Spark 中的依赖问题

java - 无法在java中将字符串日期解析为时间戳

c++ - 具有模板化基类 : Base<Derived> 的菱形继承(钻石问题)