试图简化我的程序我正在尝试创建此测试程序以测试我的其他类的添加和删除方法并打印数组列表中的属性列表我被困在如何继续
请帮助
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);
}
}
这是我的公寓类
中的添加方法public void addApartment(Apartment newApartment)
{
Apartment ApartmentInput = new Apartment();
ApartmentInput = newApartment;
Arraylist.add(ApartmentInput);
}
答案 0 :(得分:0)
假设您的公寓列表名称为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()
代码块无关紧要,因为您立即使用输入参数ApartmentInput
中的值重新分配newApartment
。同样,ApartmentInput
局部变量是多余的,因为您已经拥有newApartment
中的值。
希望这很有用。
我假设Arraylist是ArrayList类的实例的名称,如果没有,则放
ArrayList<Apartment> apartmentList = new ArrayList<>();
在类定义中的某个地方。
答案 1 :(得分:0)
您的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);
}
OR
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);
}
请注意,在这两种方法中,您将公寓添加到Company
类的实例数组列表中。这个ArrayList
需要在类的构造函数中初始化,以便您可以随时调用它的add方法。方法add不是静态方法,因此调用ArrayList.add()
不起作用(除非我弄错了)。因此,请在您的阵列列表实例中跟踪您添加的公寓。该类应该具有全局变量ArrayList<Apartment> instanceOfArrayList;
,构造函数应该初始化它:
instanceOfArrayList = new ArrayList<Apartment>();
最后,要打印有关公寓的所有信息,您可以循环遍历ArrayList中的所有元素(instanceOfArrayList.size()
),并以一种很好的方式打印出每个Apartment
的信息。选择。