【问题标题】:How do I continue to add items to array and print如何继续将项目添加到数组并打印
【发布时间】:2014-12-15 13:42:23
【问题描述】:

试图简化我的程序我正在尝试创建这个测试器来测试我的其他类的添加和删除方法并打印数组列表中的属性列表我被困在如何继续

请帮忙

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

这是我的 Apartment Class 中的 add 方法

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

【问题讨论】:

  • 我认为你的代码还没有编译,我在看main方法,它看起来还没有完成,你能编辑一下吗?
  • 好多了,谢谢,给我几分钟,我有事给你,我现在有点忙
  • 你能发布一个可编译的代码吗?
  • 我正在尝试修复代码我不确定为什么代码无法编译我正在尝试修复它,我会在发现问题后立即发布。
  • 那没关系,我想他们抓住了你

标签: java arrays inheritance arraylist casting


【解决方案1】:

您的addApartment 方法需要在您的Company 类中(您所说的类将包含公寓属性列表,以及包含ArrayList 的类)。如果你想运行你注释掉的代码,那么你的addApartment 方法需要将它的参数从Apartment 更改为你指定的“组成”这个公寓的参数。

testingCompany.addApartment(Apartmentneighbourhood, price, numberofbaths, numberofbedrooms, squarefoot, floornumbers);

或者您需要将注释掉的代码更改为

Apartment newApartment = new Apartment(Apartmentneighbourhood, price, numberofbaths, numberofbedrooms, squarefoot, floornumbers);
testingCompany.addApartment(newApartment);

最后,您需要更改 addApartment 方法以接收正确的变量,和/或使其使用输入 Apartment 而不是创建新变量。

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

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

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

instanceOfArrayList = new ArrayList<Apartment>();

最后,要打印有关公寓的所有信息,您只需遍历 ArrayList (instanceOfArrayList.size()) 中的所有元素,并以您选择的好方法打印出每个 Apartment 的信息。

【讨论】:

    【解决方案2】:

    假设您的公寓列表名称为 Arraylist(不建议这样做)

    for(Apartment apartment in Arraylist) {
        System.out.println(apartment.toString());
    }
    

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

    另一个警告 - 方法

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

    有一些问题。您没有遵循正常的 Java 命名约定。变量应以小写字母开头。 new Apartment() 代码块无关紧要,因为您立即使用输入参数 newApartment 中的值重新分配 ApartmentInput。同样,ApartmentInput 局部变量是多余的,因为您已经拥有 newApartment 中的值。

    希望这是有用的。

    我假设 Arraylist 是 ArrayList 类的一个实例的名称,如果不是,那么放

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

    类定义中的某处。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-24
      • 2017-05-02
      • 2019-05-27
      • 1970-01-01
      • 2018-08-18
      • 2015-06-12
      • 2011-07-01
      • 1970-01-01
      相关资源
      最近更新 更多