Java 添加元素后arraylist.size为0的问题

Java 添加元素后arraylist.size为0的问题,java,arraylist,constructor,Java,Arraylist,Constructor,我已经决定为我在java的半年项目编写独占程序。问题是我在数据库中有街道,所有街道都应该使用构造函数推送到ArrayList中,但是.size()方法一直说大小为零。让我示范一下: static ArrayList<Building> Buildings = new ArrayList<Building>(); //my constructor: public Building(String color, Object owner, int cost, int ren

我已经决定为我在java的半年项目编写独占程序。问题是我在数据库中有街道,所有街道都应该使用构造函数推送到ArrayList中,但是.size()方法一直说大小为零。让我示范一下:

 static ArrayList<Building> Buildings = new ArrayList<Building>();

//my constructor:
public Building(String color, Object owner, int cost, int rent, int location, String name) {
        this.color = color;
        this.owner = owner;
        this.cost = cost;
        this.rent = rent;
        this.location = location;
        this.name = name;
        Buildings.add(this);// this should add the street into the ArrayList Buildings
    }
//and a example of a street:
 Building street1 = new Building("brown", 0, 60, 2, 1, "4th street");
令我惊讶的是,它是0。 由于我的许多其他方法都使用此arraylist的大小,因此我需要它不为0。
如何实现这一点?

定义和初始化street1的区域可能不正确(检查代码行是否被调用)。 上面提到的代码对我很有用:

public class Building {

    static ArrayList<Building> Buildings = new ArrayList<Building>();
    private String color;
    private Object owner;
    private int cost;
    private int rent;
    private int location;
    private String name;

    //my constructor:
    public Building(String color, Object owner, int cost, int rent, int location, String name) {
            this.color = color;
            this.owner = owner;
            this.cost = cost;
            this.rent = rent;
            this.location = location;
            this.name = name;
            Buildings.add(this);// this should add the street into the ArrayList Buildings
        }

    public static void main(String[] args) {
         Building street1 = new Building("brown", 0, 60, 2, 1, "4th street");
         System.out.println(Building.Buildings.size());
    }   
}
公共类建筑{
静态ArrayList Buildings=新建ArrayList();
私有字符串颜色;
私人物品所有人;
私人成本;
私人租金;
私密地点;
私有字符串名称;
//我的构造函数:
公共建筑(字符串颜色、对象所有者、整数成本、整数租金、整数位置、字符串名称){
这个颜色=颜色;
this.owner=所有者;
成本=成本;
这个。租金=租金;
这个位置=位置;
this.name=名称;
建筑物。添加(此);//这应该将街道添加到ArrayList建筑物中
}
公共静态void main(字符串[]args){
建筑街1=新建筑(“布朗”,0、60、2、1,“第四街”);
System.out.println(Building.Buildings.size());
}   
}

此代码也适用于我。其他人是对的。当您到达调试点(列表的大小为0)时,可能尚未调用用于构建的构造函数。

好的, 你需要让街道在主干道上,你需要先运行主干道。
这是我这边一个愚蠢的问题。我感谢所有帮助过我的人

这里没有足够的信息。可能是创建建筑的代码从未被调用。请创建一个。还要注意的是,根据Java语言惯例,您不应该使用大写字母来命名变量。它应该是
建筑物
,而不是
建筑物
。它适用于我和你的代码我以前在大街上过,不起作用,我会尽快在pc上检查,在创建对象(建筑物)时没有调用构造函数??这就是问题所在。你是对的,应该是。但是,当您执行这个“单独的debug.java文件时,我可能会询问Building类Buildings ArrayList的大小”,构造函数可能还没有被调用。很高兴你抓到了。
public class Building {

    static ArrayList<Building> Buildings = new ArrayList<Building>();
    private String color;
    private Object owner;
    private int cost;
    private int rent;
    private int location;
    private String name;

    //my constructor:
    public Building(String color, Object owner, int cost, int rent, int location, String name) {
            this.color = color;
            this.owner = owner;
            this.cost = cost;
            this.rent = rent;
            this.location = location;
            this.name = name;
            Buildings.add(this);// this should add the street into the ArrayList Buildings
        }

    public static void main(String[] args) {
         Building street1 = new Building("brown", 0, 60, 2, 1, "4th street");
         System.out.println(Building.Buildings.size());
    }   
}