-4

嘿,我想遍历飞机以获取所有乘客并将它们添加到计数中以显示所有飞机的所有乘客。但我收到一个错误:无法迭代数组或实例。

这是方法:

public int getAllPassengers()
    {
        int passengers = 0;
        for(Plane plane : plane.getPassengerNumber())
        {
            passengers += plane.getPassengerNumber();
        }
        return passengers;
    }

飞机

import java.util.LinkedList;

public class Plane implements Comparable
{   
    private String flightNumber;
    public String airlineName;
    private double fuelRemaining;
    private int overdue;
    private int passengerNumber;
    private AIRPLANETYPE planeType;
    private boolean isLanded = false;

    public enum AIRPLANETYPE
    {
        AIRBUS("1"), CORPORATE("2"), PRIVATE("3");

        private String planeName;

        private AIRPLANETYPE(String planeName)
        {
            this.planeName = planeName;
        }

        public String getPlaneName()
        {
            return this.planeName;
        }
    }

    public Plane(String flightNumber, String airlineName,
           double fuelRemaining, int overdue, int passengerNumber, 
           AIRPLANETYPE planeType, boolean isLanded) 
    {
        this.flightNumber = flightNumber;
        this.airlineName = airlineName;
        this.fuelRemaining = fuelRemaining;
        this.passengerNumber = passengerNumber;
        this.overdue = overdue;
        this.planeType = planeType;
        this.isLanded = isLanded;
    }


    public Plane() 
    {

    }


    public String getAirlineName() {
        return airlineName;
    }

    public void setAirlineName(String airlineName) {
        this.airlineName = airlineName;
    }


    public void setOverdue(int overdue) {
        this.overdue = overdue;
    }

    public int getOverdue(){
        return overdue;
    }

    public String getFlightNumber() {
        return flightNumber;
    }

    public void setFlightNumber(String flightNumber) {
        this.flightNumber = flightNumber;
    }

    public double getFuelRemaining() {
        return fuelRemaining;
    }

    public void setFuelRemaining(double fuelRemaining) {
        this.fuelRemaining = fuelRemaining;
    }

    public int getPassengerNumber() {
        return passengerNumber;
    }

    public void setPassengerNumber(int passengerNumber) {
        this.passengerNumber = passengerNumber;
    }

    public AIRPLANETYPE getPlaneType() {
        return planeType;
    }

    public void setPlaneType(AIRPLANETYPE planeType) {
        this.planeType = planeType;
    }


    public boolean isLanded() {
        return isLanded;
    }


    public void setLanded(boolean isLanded) {
        this.isLanded = isLanded;
    }


    public int compareTo(Object arg0) {
        if((arg0 != null) && (arg0 instanceof Plane))
        {
            Plane p = (Plane) arg0;
            return (int)Math.ceil(this.overdue - p.getOverdue());
        }
        return 0;
    }
    public String toString() {
        return "Plane: flightNumber=" + flightNumber + "."
                + " airlineName=" + airlineName + "."
                + " fuelRemaining=" + fuelRemaining + " litres."
                + " overdue=" + overdue + " minutes."
                + " passengerNumber="+ passengerNumber + "."
                + " airplaneType=" + planeType +
                "hasLanded=" + isLanded+ ".\n";
    }
}
4

3 回答 3

5

passengerNumber是一个int。您需要遍历Iterable诸如ArrayList

for (Plane plane: myPlaneList) {
  passengers += plane.getPassengerNumber();
}
于 2012-12-13T14:28:53.100 回答
4

您正在尝试遍历 Plane 对象,但您的集合只是一个 int。您需要一组平面对象

int passengers = 0;
for(Plane plane : myPlanes)
{
    passengers += plane.getPassengerNumber();
}
于 2012-12-13T14:30:31.673 回答
3

您正在尝试迭代 a int,您需要迭代 anjava.util.Iterable

您发布的代码不包含回答此问题所需的信息。

您正在向我们展示 class Plane,但是为了拥有多个平面,您可能在代码中的其他位置有 aList<Plane>或 a 。Plane[]这是一个可行的示例:

public class Main {
  List<Plane> allPlanes; // Load in the omitted code somewhere else

  public int getAllPassengers()
  {
    int passengers = 0;
    for(Plane plane : allPlanes) // note the change
    {
        passengers += plane.getPassengerNumber();
    }
    return passengers;
  }
}
于 2012-12-13T14:29:18.347 回答