简体   繁体   English

初始化的ArrayList不显示

[英]Initialized ArrayList doesn't show up

I have this code of a larger assignment. 我有一个较大的代码。 I have omitted any unnecessary code. 我省略了任何不必要的代码。 The thing is that I'm asked to initialize the ArrayList by adding objects to it. 问题是我被要求通过向其添加对象来初始化ArrayList。 However, when I type 3(the option to show the buildings), nothing is shown but the menu again. 但是,当我键入3(显示建筑物的选项)时,仅菜单再次显示。 Any help will be appreciated! 任何帮助将不胜感激!

PS I do NOT ask you to solve my assignment. 附言:我不要求您解决我的任务。 We have done similar smaller case examples in the labs of my uni. 我们在uni的实验室中做了类似的较小案例示例。 However, I can't get it to work for some reason and I can't find out why. 但是,由于某种原因,我无法使它正常工作,而且我也找不到原因。

import java.util.*;

public class mainApp {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        BuildingList myBList = new BuildingList();
        ExpenseList myExpList = new ExpenseList();
        BuildingExpenseList myExpTList = new BuildingExpenseList();
        boolean done = false;

        while (!done)
        {
            System.out.println("\n1. Insert new building");
            System.out.println("2. Insert new expense");
            System.out.println("3. Show all buildings");
            System.out.println("4. Show the expenses for a building");
            System.out.println("5. Calculate the cost for a building");
            System.out.println("6. Calculate the cost of an expense");
            System.out.println("0. Exit");
            System.out.print("> ");
            answer = in.nextLine();

            if (answer.equals("3"))
            {
            myBList.showBuilds();
            }
            else if (answer.equals("0")) done = true;
       }

       Building b1 = new Building("1", "Offices", "5th Avenue", 130, 340);
       Building b2 = new Building("2", "Shop", "St. John", 100, 200);
       Building b3 = new Building("3", "Fire Station", "Weston Road", 250, 550);
       Building b4 = new Building("4", "Police Station", "Two Bridges Road", 700, 1000);
       Building b5 = new Building("5", "Restaurant", "St Nicholas Avenue", 400, 600);

       myBList.addBuilding(b1);
       myBList.addBuilding(b2);
       myBList.addBuilding(b3);
       myBList.addBuilding(b4);
       myBList.addBuilding(b5);
  }
}

import java.util.ArrayList;

public class BuildingList
{
    ArrayList<Building> Buildings = new ArrayList<Building>();

    public void addBuilding(Building b)
    {
        Buildings.add(b);
    }
    public void showBuilds()
    {
        for (Building b : Buildings)
        {
        System.out.println(b.printBuilding());
        }
    }
}

public class Building
{
    String code;
    String description;
    String address;
    float areaPrice;
    float sqM;

    public Building (String code, String description, String address, float areaPrice, float sqM)
    {
    this.code = code;
    this.description = description;
    this.address = address;
    this.areaPrice = areaPrice;
    this.sqM = sqM;
    }
    public String printBuilding()
    {
        return "Code " +code+ "\t Description " +description+ "\t Address " +address+ "\t Area Price ";
    }

The problem is that the list is never filled when you press "3": 问题是,当您按“ 3”时,列表永远不会填充:

This Code: 此代码:

   Building b1 = new Building("1", "Offices", "5th Avenue", 130, 340);
   Building b2 = new Building("2", "Shop", "St. John", 100, 200);
   Building b3 = new Building("3", "Fire Station", "Weston Road", 250, 550);
   Building b4 = new Building("4", "Police Station", "Two Bridges Road", 700, 1000);
   Building b5 = new Building("5", "Restaurant", "St Nicholas Avenue", 400, 600);

   myBList.addBuilding(b1);
   myBList.addBuilding(b2);
   myBList.addBuilding(b3);
   myBList.addBuilding(b4);
   myBList.addBuilding(b5);

Must be implemented before the while loop. 必须在while循环之前实现。

Also, try implementing your builder class like this, constructing the ArrayList in the objects constructor. 另外,尝试像这样实现您的构建器类,在对象构造器中构造ArrayList。

public class BuildingList
{
    List<Building> Buildings;

    public BuildingList() {
        Buildings = new ArrayList<Building>();
    }

    public void addBuilding(Building b)
    {
        Buildings.add(b);
    }
    public void showBuilds()
    {
        for (Building b : Buildings)
        {
            System.out.println(b.printBuilding());
        }
    }
}

And if you want to look really cool for your professor, you can substring the ArrayList.toString() method, remove and replace the commas with the new line character "\\n". 而且,如果您想让您的教授看起来真的很酷,可以对ArrayList.toString()方法进行子字符串化,删除并用换行符“ \\ n”替换逗号。 You can do this in one System.out.println() call, as opposed to a loop 您可以在一个System.out.println()调用中执行此操作,而不是循环

There is no way it could print the list because the values are inserted after the loop execution. 它无法打印列表,因为在循环执行后插入了这些值。
Cut all the code that puts the values in the myBList, paste it above the loop and execute it. 剪切所有将值放入myBList的代码,将其粘贴到循环上方并执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM