开发者

Access object.variable in an array of objects

I need help with this piece of code.

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);

    }
}

I am storing a Car Object in Slot. I wish to print/access the No and Colour of the car stored in slot. How do I go about doing that?


Well, if car has a public property, or a public getter method (this is preferable - getNumber() and getColour()), you can call them while iterating the array with the for-each loop:

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

Note that I've lowercased slot - variable names in Java should be lowercase. I'd also advise for naming the array with plural name - i.e. slots.

Note also that the way of iteration provided by others is possible, but not recommended for the basic case of iterating the whole array. Effective Java (Bloch) recommends using the foreach loop whenever possible.


Using [] notation:

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());
        }
    }
}

(I assumed by the name that getNo was a method, not a property.)

E.g., Slot[0] gives you the first Car, from which you can access Car's properties and methods, so Slot[i] gives you the car at the ith position. (In the above I used a temporary variable to store the car, but you can use Slot[i].getNo() directly, it doesn't matter. I just didn't want to repeat the array lookup, even through HotSpot [the Sun JVM] will optimize it out even if I do.)


Sorry for being so late. I noticed something missing in the above answers, so here is the complete solution for the problem stated.

Here is the ParkingLot class with a call to Allot() method.

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

    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());
        }
    }
}

}

And the Car class with the getNo() and getColor() methods.

public class 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;
}

}


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

}


you can create and access object array of objects simply like this

Object[] row={"xx","xcxcx"};

Object[] cotainer = {row,row,row};

for(int a=0;a<cotainer.length;a++){

    Object[] obj = (Object[])cotainer[a];

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜