初始化的ArrayList不会显示

时间:2018-04-22 02:18:44

标签: java arraylist

我有一个更大的任务代码。我省略了任何不必要的代码。问题是我被要求通过向其添加对象来初始化ArrayList。但是,当我输入3(显示建筑物的选项)时,除了菜单之外没有显示任何内容。任何帮助将不胜感激!

P.S。我不要求你解决我的任务。 我们在我的大学实验室里做过类似的小案例。但是,由于某些原因我无法让它工作,我找不到原因。

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 ";
    }

2 个答案:

答案 0 :(得分:1)

问题是当你按“3”时,列表永远不会被填充:

本准则:

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());
        }
    }
}

必须在while循环之前实现。

另外,尝试像这样实现构建器类,在对象构造函数中构造ArrayList。

from pandas import DataFrame, date_range, Timedelta
import numpy as np
from matplotlib import pyplot as plt

rng = date_range(start='2015-01-01', periods=5, freq='24H')
df = DataFrame({'y':np.random.normal(size=len(rng))}, index=rng)
y1 = df['y']
y2 = (y1*3)
sd1 = (y1*2)
sd2 = (y1*2)

fig,(ax1,ax2) = plt.subplots(2,1,sharex=True)

_ = y1.plot(yerr=sd1, ax=ax1)
_ = y2.plot(yerr=sd2, ax=ax2)

如果你想为你的教授看起来很酷,你可以对ArrayList.toString()方法进行子串,删除并用新行字符“\ n”替换逗号。您可以在一个System.out.println()调用中执行此操作,而不是循环

答案 1 :(得分:0)

无法打印列表,因为在循环执行后插入了值。
剪切所有将值放在myBList中的代码,将其粘贴到循环上方并执行它。