2012-12-13 117 views
-4

嘿,我想通過飛機循環獲取所有乘客,並將它們添加到計數顯示所有飛機的所有乘客。但即時通訊出現錯誤:無法遍歷數組或實例。無法通過循環getAllPassengers

這裏是方法:

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"; 
    } 
} 

回答

3

您試圖遍歷一個int,你需要遍歷一個java.util.Iterable

您發佈的代碼不包含的信息需要回答這個問題。

您向我們展示類Plane,但爲了有一個以上的飛機,你可能有一個List<Plane>或代碼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; 
    } 
} 
5

passengerNumberint。您需要遍歷一個IterableArrayList

​​
+0

究竟是一個可迭代的東西。 – user1900917

+1

實現'Iterable'並因此具有'iterator()'方法的東西。請參閱http://docs.oracle.com/javase/6/docs/api/java/lang/Iterable.html – atamanroman

+0

這是一個[ìnterface](http://docs.oracle.com/javase/7/docs/api /java/lang/Iterable.html)由所有常用的[Collections](http://docs.oracle.com/javase/tutorial/collections/index.html)實現。 – Reimeus

4

您試圖通過平面物體進行迭代,但您的收藏僅僅是一個int。您需要一組平面對象

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