【发布时间】:2022-01-05 06:49:58
【问题描述】:
我对 java 还很陌生,找不到特别能回答我问题的问题。
我将一个数组列表初始化为实例变量,并且在一种方法中我编辑了这个也有效的数组列表,但是我无法将此值存储在实例变量中,然后用另一种方法读取它,因为它再次显示为空。我尝试使用 getter 和 setter,但这并没有真正起作用,即使我认为这是解决方案的一部分......
这是我的代码的一些相关的sn-ps:
public class CarManager {
ArrayList<Car> carList = new ArrayList<>();
String carId ="";
String driver= "";
int sizeCategory= 0;
Car myCar= new Car (carId,driver,sizeCategory);`
public void addCar () {
CarManager myCarManager= new CarManager();
Car car1 = new Car(carId,driver,sizeCategory);
carId= car1.getCarId(carId);
driver= car1.getDriver (driver);
sizeCategory= car1.getSizeCategory (sizeCategory);
System.out.println("You entered the following data:");
System.out.println("\ncar ID: "+ carId);
System.out.println("driver's name:" +driver);
System.out.println("size category: "+sizeCategory);
System.out.println("\nIf you are okay with this, press 0 to save the data and return to the CARS MENU");
System.out.println("\nIf you made a mistake, press 1 to enter the data again");
Scanner afterAddCar= new Scanner(System.in);
String choice1;
choice1= afterAddCar.next().trim();
if ("1".equals (choice1)) {
myCarManager.addCar();
}
if ("0".equals (choice1)) {
Car returnedCar= new Car (carId,driver,sizeCategory);
carList.add(returnedCar);
String list = carList.toString();
System.out.println(list);
myCarManager.setCarList(carList);
ArrayList<Car> afterAdd= carList;
myCarManager.handleCars();
}
}
public ArrayList<Car> getCarList() {
return carList;
}
public void setCarList(ArrayList<Car> carList) {
this.carList = carList;
}
public void listCars () {
String list = carList.toString();
System.out.println(list);
}
我还有第二节课叫做 Car,为了更好地理解,我也会在这里发布:
public class Car {
private String carId;
private String driver;
private int sizeCategory; // value from 1-3 indicating the size of the car
public Car () {}
public Car (String carId,String driver,int sizeCategory){
this.carId= carId;
this.driver= driver;
this.sizeCategory= sizeCategory;
}
public String getCarId(String carId) {
Scanner keyboard = new Scanner(System.in);
System.out.println("\nFirst enter the car ID: " + carId);
while (carId.length() < 6) {
System.out.println("Please enter an ID that has at least 6 characters");
carId = keyboard.next();
}
return carId;
}
public void setCarId(String carId) {
this.carId = carId;
}
public String getDriver(String driver) {
Scanner keyboard = new Scanner(System.in);
System.out.println("\nNext enter the driver's name:" + driver);
driver= driver.trim();
while (driver.length() < 2) {
System.out.println("Please enter a name that has at least 2 characters");
driver = keyboard.next();
driver= driver.trim();
}
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public int getSizeCategory(int sizeCategory) {
Scanner keyboard = new Scanner(System.in);
String input="";
System.out.println("\nNow the size category: ");
while (sizeCategory <1 || sizeCategory >3) {
System.out.println("The size category has to be between one and three");
input = keyboard.next().trim();
try {
sizeCategory = Integer.parseInt(input);
} catch (Exception e) {
sizeCategory = 0;
}
}
return sizeCategory;
}
public void setSizeCategory(int sizeCategory) {
this.sizeCategory = sizeCategory;
}
现在我知道这里可能有不止一件事可以改进,但我最感兴趣的是通过列出更新后的数组列表来解决问题。
提前非常感谢!
【问题讨论】:
标签: java arraylist instance-variables