2

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?

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It's "syntactic sugar" - the loop iterates over all elements of the slot array/collection. Generally, slot should implement Iterable, but this is also true for arrays.
Also, method names should be lowercased too. The "Allot()" method should be renamed to "allot()"
1

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

1 Comment

In his case I would add a if( Slot[i] != null ).
1

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

}

Comments

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

}

Comments

0

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.