Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 访问对象数组中的object.variable_Java - Fatal编程技术网

Java 访问对象数组中的object.variable

Java 访问对象数组中的object.variable,java,Java,我需要这段代码的帮助 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() {

我需要这段代码的帮助

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

    }
}
公共级停车场{
静态int MAX=5;
静态车[]槽=新车[最大值];
公共静态void main(字符串[]args){
槽[0]=新车(“1234”、“白色”);
槽[1]=新车(“5678”、“黑色”);
}
公共静态无效分配(){

对于(int i=0;i使用
[]
符号:

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());
        }
    }
}
publicstaticvoidalloct(){
汽车;

对于(int i=0;i嗯,如果
car
具有公共属性或公共getter方法(最好是-
getNumber()
getcolor()
),则可以在使用for each循环迭代数组时调用它们:

for (Car car : slot) {
    System.out.println(car.getColour());
}
请注意,我已将
插槽
-Java中的变量名称应为小写。我还建议使用复数名称命名数组-即
插槽


还请注意,其他人提供的迭代方式是可能的,但不推荐用于迭代整个数组的基本情况。建议尽可能使用foreach循环。

很抱歉这么晚。我注意到上面的答案中缺少一些内容,因此这里是所述问题的完整解决方案

下面是ParkingLot类,它调用Allot()方法

公共停车场{

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

}

您可以像这样简单地创建和访问对象数组

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

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

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

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

}
Object[]行={“xx”,“xcx”};
Object[]cotainer={row,row,row};

对于(int a=0;在他的情况下,我会添加一个if(Slot[I]!=null)。这是“语法糖”-循环迭代
Slot
数组/集合的所有元素。通常,
Slot
应该实现
Iterable
,但对于数组也是如此。此外,方法名称也应该是小写的。“Allot()”方法应重命名为“allot()
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);
    }

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

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

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

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

}