我有一个类型为car的数组列表。
我有一个重要的#include "testfile.h"
int var1 = 10;
void testFcn(void){
int var3 = var1;
}
方法,以所需的格式打印我的汽车。
我使用toString
方法打印汽车。
我现在只有一个它可以工作,但我希望它打印数组列表中的所有汽车。
这是我的代码:
arraylist.get(index)
我还放了public class Garage {
private static int carsCapacity = 30;
ArrayList<Cars> myGarage;
public Garage() {
Scanner scanAmountOfCars = new Scanner(System.in);
System.out.println("Please limit the number of car the garage can contain.");
System.out.println("If greater than 50, limit will be 30.");
int validAmount = scanAmountOfCars.nextInt();
if (validAmount <= 50) {
carsCapacity = validAmount;
myGarage = new ArrayList<Cars>();
}
System.out.println(carsCapacity);
}
public void addCar() {
Cars car = new Cars(Cars.getID(), Cars.askCarID(), Cars.getPosition(), Attendant.askForAtt(), System.currentTimeMillis());
myGarage.add(car);
//System.out.println(car);
}
public static int getCarsCapacity() {
return carsCapacity;
}
@Override
public String toString() {
return "Garage [Car:" + myGarage.get(0).getPlateNum() + " ID:" + myGarage.get(0).getCarID() + " Position:" + Cars.getPosition() +
" Assigned to:" + Cars.getAssignedTo().getId() + "(" + Cars.getAssignedTo().getName()
+ ")" + " Parked at:" + Cars.convert(myGarage.get(0).getCurrTime()) + "]";
}
}
课,以防你需要它:
Cars
答案 0 :(得分:3)
您的Garage
应该使用toString()
实施ArrayList#toString()
:
public String toString() {
return "Garage: " + myGarage.toString();
}
另请记住在toString()
中实施Cars.java
。
public String toString() {
return "[" + this.carID + " " +
this.plateNum + " " +
Cars.position + " " +
Cars.assignedTo.toString + " " +
String.valueOf(this.currTime) + "]"
}