Java、数组列表、项目

问题描述 投票:0回答:1

我写了一个函数,必须:

  1. 提示用户输入对象数据。

  2. 在对象上设置这些属性。

  3. 将新对象添加到相对列表中。

    public static void addNewVehicle(MainVehicle 车辆, String 类型){ 扫描仪阅读器 = new Scanner(System.in);

     System.out.println("Please, enter the make of the " + type + ":");
     vehicle.setMake(reader.next());
     System.out.println("Please, enter the model of the " + type + ":");
     vehicle.setModel(reader.next());
     System.out.println("Please, enter the year of manufacture:");
     vehicle.setYearOfManufacture(reader.nextInt());
     if (vehicle instanceof Car){
         System.out.println("Pleas, enter the number of doors:");
         ((Car) vehicle).setNumberOfDoors(reader.nextInt());
         System.out.println("Please, enter the fuel type:");
         ((Car) vehicle).setFuelType(reader.next());
         carList.add((Car) vehicle);
     } else if (vehicle instanceof MotorCycle) {
         System.out.println("Pleas, enter the number of wheels:");
         ((MotorCycle) vehicle).setNumberOfWheels(reader.nextInt());
         System.out.println("Please, enter the type of the motorcycle");
         ((MotorCycle) vehicle).setMotorCycleType(reader.next());
         motorCyclesList.add((MotorCycle) vehicle);
     } else if (vehicle instanceof Truck) {
         System.out.println("Pleas, enter the cargo capacity in tons:");
         ((Truck) vehicle).setCargoCapacityTons(reader.nextInt());
         System.out.println("Please, enter the transmission type:");
         ((Truck) vehicle).setTransmissionType(reader.next());
         truckList.add((Truck) vehicle);
         System.out.println(truckList.get(0).getMake());
     }
    

我面临的问题是: 当我添加多个对象并尝试打印所有对象的数据时,我得到了我添加了很多条的最后一个对象的数据。

例如: 如果我添加具有以下数据的汽车: 1 1 1 1,0 1 并添加另一辆车并具有以下数据: 2 2 2 2,0 2

如果我要求程序打印出第一个对象,我期望得到: 1 1 1 1,0 1

但我实际得到的是: 2 2 2 2,0 2

如果我要求程序循环处理项目并获取所有数据,我期望这样: 1 1 1 1,0 1 2 2 2 2,0 2

但我明白了: 2 2 2 2,0 2 2 2 2 2,0 2

我尝试做的事情: 我尝试将 add() 方法更改为 addFirst() 或 addLast()。 我尝试将 get() 方法更改为 getLast() 或 getFirst()。

我不知道这个问题有什么关系。 我认为问题与此功能有关,但如果不是,请告诉我,以便我提供更多代码。

感谢您的帮助!

java loops arraylist overwrite items
1个回答
0
投票

要解决此问题,每次添加新车辆时,您都需要创建 MainVehicle(及其子类,如 Car、MotorCycle、Truck)的新实例。这是修改后的函数:

导入java.util.Scanner; 导入java.util.List; 导入 java.util.ArrayList;

公共类VehicleManager { // 存储不同类型车辆的列表 私有静态列表 carInventory = new ArrayList<>(); 私有静态列表 motorCycleInventory = new ArrayList<>(); 私有静态列表 TruckInventory = new ArrayList<>();

public static void addNewVehicle(String category) {
    Scanner input = new Scanner(System.in);
    
    // Create a new instance of the vehicle based on the category
    MainVehicle newVehicle;
    if ("Car".equalsIgnoreCase(category)) {
        newVehicle = new Car();
    } else if ("MotorCycle".equalsIgnoreCase(category)) {
        newVehicle = new MotorCycle();
    } else if ("Truck".equalsIgnoreCase(category)) {
        newVehicle = new Truck();
    } else {
        System.out.println("Sorry, we don't recognize that vehicle category.");
        return;
    }

    // Set the common properties of the vehicle
    System.out.println("Enter the make of the " + category + ":");
    newVehicle.setMake(input.next());
    System.out.println("Enter the model of the " + category + ":");
    newVehicle.setModel(input.next());
    System.out.println("Enter the year it was manufactured:");
    newVehicle.setYearOfManufacture(input.nextInt());

    // Set the specific properties based on the vehicle type
    if (newVehicle instanceof Car) {
        Car car = (Car) newVehicle;
        System.out.println("Enter the number of doors:");
        car.setNumberOfDoors(input.nextInt());
        System.out.println("Enter the type of fuel it uses:");
        car.setFuelType(input.next());
        carInventory.add(car);
    } else if (newVehicle instanceof MotorCycle) {
        MotorCycle motorCycle = (MotorCycle) newVehicle;
        System.out.println("Enter the number of wheels:");
        motorCycle.setNumberOfWheels(input.nextInt());
        System.out.println("Enter the type of motorcycle:");
        motorCycle.setMotorCycleType(input.next());
        motorCycleInventory.add(motorCycle);
    } else if (newVehicle instanceof Truck) {
        Truck truck = (Truck) newVehicle;
        System.out.println("Enter the cargo capacity in tons:");
        truck.setCargoCapacityTons(input.nextInt());
        System.out.println("Enter the type of transmission:");
        truck.setTransmissionType(input.next());
        truckInventory.add(truck);
    }
} }
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.