有 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() {
    for (int i = 0; i <= Slot.length; i++) {
        System.out.println(Slot.getNo);

    }
}

我正在Car中存储一个Slot对象。我希望打印/访问存储在插槽中的汽车的NoColour。我该怎么做呢


共 (5) 个答案

  1. # 1 楼答案

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

    Object[] row={"xx","xcxcx"};
    
    Object[] cotainer = {row,row,row};
    
    for(int a=0;a<cotainer.length;a++){
    
        Object[] obj = (Object[])cotainer[a];
    
    }
    
  2. # 2 楼答案

    好的,如果car有一个公共属性,或者有一个公共getter方法(最好是getNumber()getColour()),可以在使用for each循环迭代数组时调用它们:

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

    注意,我已经将slot小写了——Java中的变量名应该是小写的。我还建议用复数名命名数组,即slots

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

  3. # 3 楼答案

    抱歉这么晚了。我注意到上面的答案中缺少了一些东西,所以下面是针对所述问题的完整解决方案

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

    }

    以及带有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;
    }
    

    }

  4. # 4 楼答案

    使用[]符号:

    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位的汽车。(在上面的例子中,我使用了一个临时变量来存储汽车,但是你可以直接使用Slot[i].getNo(),这没关系。我只是不想重复数组查找,即使通过HotSpot[Sun JVM]来优化,即使我这样做了。)

  5. # 5 楼答案

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