Java 如何继续将项目添加到阵列并打印

Java 如何继续将项目添加到阵列并打印,java,arrays,inheritance,arraylist,casting,Java,Arrays,Inheritance,Arraylist,Casting,试图简化我的程序我正在尝试创建这个测试仪来测试我的其他类的add和remove方法,并打印数组列表中的属性列表我一直在思考如何继续 请帮忙 public class Tester { public static void main (String[] args) { } public Tester () { Apartment testingApartment = new Apartment(); Basement tes

试图简化我的程序我正在尝试创建这个测试仪来测试我的其他类的add和remove方法,并打印数组列表中的属性列表我一直在思考如何继续

请帮忙

public class Tester
{
    public static void main (String[] args)
    {
    }

    public Tester ()
    {
        Apartment testingApartment = new Apartment();
        Basement testingBasement = new Basement();
        Company testingCompany = new Company();// Contains list of
                                                // properties to sell
                                                // in arraylist

        // elements with a 2 after the name are used for the Basement
        // elements

        String Apartmentneighbourhood = "Parkdale";
        String Basementneighbourhood = "Rexdale";
        double price = 400000.99;
        double price2 = 5000000.99;
        int numberofbaths = 3;
        int numberofbaths2 = 5;
        int numberofbedrooms = 2;
        int numberofbedrooms2 = 4;
        int squarefoot = 3000;
        int squarefoot2 = 5000;
        int floors = 4;
        int floorlevel = 4;

        testingApartment.setneighbourhood(apartmentneighbourhood);
        testingBasement.setneighbourhood(Basementneighbourhood);
        testingApartment.setprice(price);
        testingBasement.setprice(price2);
        testingApartment.setbathNum(numberofbaths);
        testingBasement.setbathNum(numberofbaths2);
        testingApartment.setbedNum(numberofbedrooms);
        testingBasement.setbedNum(numberofbedrooms2);
        testingApartment.setsqrft(squarefoot);
        testingBasement.setsqrft(squarefoot2);
        testingApartment.setNumFloors(floors);
        testingBasement.setFloorLevel(floorlevel);
    }
}
这是我公寓类中的add方法

public void addApartment(Apartment newApartment)
    {
      Apartment ApartmentInput = new Apartment();
        ApartmentInput = newApartment;
    Arraylist.add(ApartmentInput);
    }

假设你的公寓清单上有Arraylist(这是不可取的),那么

这将是一个开始。您可以在公寓类上实现一个toString方法来实现它

另一个警告-方法

public void addApartment(Apartment newApartment)
{
    Apartment ApartmentInput = new Apartment();
    ApartmentInput = newApartment;
    Arraylist.add(ApartmentInput);
}
有一些问题。您没有遵循正常的Java命名约定。变量应以小写字母开头。
new plant()
代码块是不相关的,因为您立即使用输入参数
newplant
中的值重新分配
ApartmentInput
。类似地,
ApartmentInput
局部变量是多余的,因为您在
newfamility
中已经有了值

希望这是有用的

我假设Arraylist是Arraylist类实例的名称,如果不是,则将

ArrayList<Apartment> apartmentList = new ArrayList<>();
ArrayList apartmentList=新建ArrayList();

在类定义中的某个地方。

您的
添加公寓
方法需要在您的
公司
类中(您所说的类将包含公寓的属性列表,以及包含
阵列列表的类
)。如果要运行注释掉的代码,则您的
addpartment
方法需要将其参数从
partment
更改为指定“组成”此单元的参数

testingCompany.addApartment(Apartmentneighbourhood, price, numberofbaths, numberofbedrooms, squarefoot, floornumbers);
或者您需要将注释掉的代码更改为

Apartment newApartment = new Apartment(Apartmentneighbourhood, price, numberofbaths, numberofbedrooms, squarefoot, floornumbers);
testingCompany.addApartment(newApartment);
最后,您需要更改
addpartment
方法以获取正确的变量,和/或使其使用输入
partment
,而不是创建新的变量

public void addApartment(Apartment newApartment) {
    instanceOfArrayList.add(newApartment);
}

请注意,在这两种方法中,都将单元添加到
公司
类中数组列表的实例中。这个
ArrayList
需要在类的构造函数中初始化,以便您可以随时调用它的add方法。add方法不是静态方法,因此调用
ArrayList.add()
应该不起作用(除非我弄错了)。因此,在数组列表的实例中跟踪添加的单元。该类应具有全局变量
ArrayList instanceOfArrayList,构造函数应初始化它:

instanceOfArrayList = new ArrayList<Apartment>();
instanceOfArrayList=newarraylist();

最后,要打印有关单元的所有信息,您只需在ArrayList(
instanceOfArrayList.size()
)中的所有元素上循环,然后以您选择的好方法打印每个
单元的信息。

我想您的代码甚至还没有编译,我正在查看主方法,看起来还没有完成,你能编辑一下吗?好多了,谢谢,给我几分钟,我会给你准备一些东西,我现在有点忙,你能发布一个可编译的代码吗?我正在尝试修复代码我不知道为什么代码没有编译我正在努力修复它,我会在发现问题后尽快发布。好吧,不管怎样,我想他们已经找到你了
public void addApartment(String nbhd, double price, int baths, int beds, int squareFt, int floors) {
    Apartment toAdd = new Apartment(nbhd, price, baths, beds, squareFt, floors);
    instanceOfArrayList.add(toAdd);
}
instanceOfArrayList = new ArrayList<Apartment>();