2010-06-26 74 views
1

我需要關於這段代碼的幫助。在對象數組中訪問object.variable

public class ParkingLot { 

static int MAX = 5; 
static Car[] Slot = new Car[MAX]; 

public static void main(String[] args) { 


    Slot[0] = new Car("1234", "White"); 
    Slot[1] = new Car("5678", "Black"); 

} 

public static void Allot() { 
    for (int i = 0; i <= Slot.length; i++) { 
     System.out.println(Slot.getNo); 

    } 
} 

我在Slot存儲Car對象。我希望打印/訪問存儲在插槽中的汽車的NoColour。我該如何去做呢?

回答

2

那麼,如果car有一個公共屬性或公共getter方法(這是可取的 - getNumber()getColour()),您可以在用for-each循環迭代數組時調用它們:

for (Car car : slot) { 
    System.out.println(car.getColour()); 
} 

請注意,我小寫slot - 在Java中變量的名字應該是小寫。我還建議以複數名稱命名陣列 - 即slots

還要注意其他人提供的迭代方式是可能的,但不推薦用於迭代整個數組的基本情況。 Effective Java (Bloch)建議儘可能使用foreach循環。

+0

嗯,(汽車:插槽)是如何工作的? – theTuxRacer 2010-06-26 07:55:47

+0

它是「語法糖」 - 循環遍歷'slot'數組/集合的所有元素。通常,'slot'應該實現'Iterable',但是對於數組也是如此。 – Bozho 2010-06-26 07:59:28

+1

另外,方法名稱也應該小寫。 「Allot()」方法應該重命名爲「allot()」 – Michael 2010-06-26 14:35:30

1

使用[]符號:

public static void Allot() { 
    Car car; 
    for (int i = 0; i <= Slot.length; i++) { 
     // Get the car at this position in the array 
     car = Slot[i]; 

     // Make sure it isn't null, since the array may not have 
     // a full set of cars 
     if (car != null) { 
      // Use the car reference 
      System.out.println(car.getNo()); 
     } 
    } 
} 

(我所假設的名稱getNo是一種方法,而不是一個屬性。)

例如,Slot[0]給你的第一Car,從中你可以訪問Car的屬性和方法,所以Slot[i]給你的車在i th的位置。 (在上面我使用了一個臨時變量來存儲汽車,但你可以直接使用Slot[i].getNo(),這並不重要,我只是不想重複數組查找,即使通過HotSpot [Sun JVM]也會優化它)

+0

在他的情況下,我想補充一個,如果(插槽[I]!= NULL)。 – InsertNickHere 2010-06-26 07:41:04

+0

@InsertNickHere:是的,好主意。 – 2010-06-26 07:42:10

1

對不起,這麼晚了。我注意到上面的答案中缺少一些東西,所以這裏是所述問題的完整解決方案。

以下是調用Allot()方法的ParkingLot類。

公共類ParkingLot {

static int MAX = 5; 
static Car[] Slot = new Car[MAX]; 

public static void main(String[] args) { 

    Slot[0] = new Car("1234", "White"); 
    Slot[1] = new Car("5678", "Black"); 

    Allot(); 

} 

public static void Allot() { 

    for (int i = 0; i < Slot.length; i++) { 

     if (Slot[i] != null) { 
      System.out.println(Slot[i].getNo()+" , "+Slot[i].getColor()); 
     } 
    } 
} 

}

而且隨着getNo()和的getColor Car類()方法。

公共類車{

private String Number; 
private String Color; 

Car (String Number, String Color){ 
    this.Number = Number; 
    this.Color = Color; 

} 

public String getNo(){ 
return Number; 
} 

public String getColor(){ 
    return Color; 
} 

}

0
class Car{ 
    String number; 
    String color; 

    public Car(String number, String color) { 
     this.number = number; 
     this.color = color; 
    } 

    @Override 
    public String toString() { 
     return "Car{" + 
       "number='" + number + '\'' + 
       ", color='" + color + '\'' + 
       '}'; 
    } 
} 

class Test{ 
    static int MAX = 5; 
    static Car[] Slot = new Car[MAX]; 

    public static void main(String[] args) { 
     Slot[0] = new Car("1234", "White"); 
     Slot[1] = new Car("5678", "Black"); 

     for (Car car : Slot) 
      System.out.println(car); 
    } 

}