将多个Java对象数组传递给另一个方法

时间:2018-06-09 11:43:38

标签: java oop

我刚开始学习java,在python和一些javascript中有一些小的经验,但没有使用类。我对此代码有疑问(仅供参考,下面我指出确切的问题):

public class Race {
    Boolean isThereABrokenTruck = false;
    private Car[] cars;
    private Motorcycle[] motorcycles;
    private Truck[] trucks;
    private void createVehicles() {
        cars = new Car[10];
        motorcycles = new Motorcycle[10];
        trucks = new Truck[10];

    } // creates 10 cars, 10 trucks and 10 motorcycles.

    private void simulateRace() {
        Weather.setRaining();
        for (Car car : cars) {
            for (int i = 0; i < 50; i++) {
                car.moveForAnHour();
            }
        }
        for (Motorcycle motorcycle : motorcycles) {
            for (int i = 0; i < 50; i++) {
                motorcycle.moveForAnHour();
            }
        }
        for (Truck truck : trucks) {
            for (int i = 0; i < 50; i++) {
                truck.moveForAnHour();
            }
        }

    } 
    private void printRaceResults() {
        for (Car car : cars) {
            System.out.println("Name: " + car.name);
            System.out.println("\n Distance Travelled: " + car.distanceTraveled);
            System.out.println("\n Type:" + car.getClass().getName());
        }
        for (Motorcycle motorcycle : motorcycles) {
            System.out.println("Name: " + motorcycle.name);
            System.out.println("\n Distance Travelled: " + motorcycle.distanceTraveled);
            System.out.println("\n Type:" + motorcycle.getClass().getName());
        }
        for (Truck truck : trucks) {
            System.out.println("Name: " + truck.name);
            System.out.println("\n Distance Travelled: " + truck.distanceTraveled);
            System.out.println("\n Type:" + truck.getClass().getName());
        }
    } // prints each vehicle's name, distance traveled ant type.

    protected Boolean isThereABrokenTruck() {
        return isThereABrokenTruck;
    }

    public static void main(String[] args) {
        Race race = new Race();
        race.createVehicles();
        race.simulateRace();
        race.printRaceResults();
    }
}

此代码编译(Car,Motorcycle和Truck类也在我的代码中定义,但与问题无关),但是我得到运行时空指针异常

   for (Car car : cars) { // null pointer exception here
        for (int i = 0; i < 50; i++) {
            car.moveForAnHour();
        }
    }

所以我想我没有正确地为汽车分配价值。我不得不使用单独的方法来创建这些车辆,对它们进行操作并将结果打印到控制台。在python中我可能只返回多个数组(或列表)并将它们的值分配给不同的变量,但是我如何在Java中这样做?

4 个答案:

答案 0 :(得分:1)

构造函数中的初始数组

public Race() {
  createVehicles();
}

答案 1 :(得分:1)

您初始化车辆阵列:

private void createVehicles() {
    cars = new Car[10];
    motorcycles = new Motorcycle[10];
    trucks = new Truck[10];

}

但是你的阵列现在只包含null-car,null-trucks ......

您还需要初始化它们:

private void createVehicles() {
    cars = new Car[10];
    for (int i = 0; i < cars.length; i++) {
        cars[i] = new Car();
    }
    motorcycles = new Motorcycle[10];
    trucks = new Truck[10];
    // Init other vehicles as well
}

答案 2 :(得分:0)

您正在创建cars变量以容纳10个Car实例但不存储任何值。向数组中添加一些值并尝试。

答案 3 :(得分:0)

初始化数组时,使用空值填充数组。 所以 Car [] cars = new Car [10]; 意味着你有一个包含10个空值的数组。而你正在迭代它。你会在这里得到NullPointerException car.moveForAnHour(); 在createVehicles()中,您必须用对象填充它。 例如:

private void createVehicles() {
    cars = new Car[10];
    for(int i = 0; i < cars.length; i++) {
      cars[i] = new Car();
    }

}