如何使用另一个数组中的对象初始化一个新数组

我在初始化数组时遇到问题。它显示了一个新的内存地址数组,而不是值。我错了,但我找不到错误。这可能很容易,但我希望你能帮助我。


public static void searchCarByBrand(Car[] cars, char ch) {


    System.out.println("the method searchCarByBrand was called with ch = " + ch);

    int carsLenght = cars.length;

    Car[] carsArray = new Car[carsLenght];


    for(Car aCars : cars) {

        System.out.println("this is for each");


        if(getBrand().charAt(0) == ch) {

            System.out.println("this is if");


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

                carsArray[i] = new Car();

                System.out.println("carsArray[" + i + "]: " + carsArray[i]);

            }

        }

    }

}

public class Car {

private static String brand;

private String model;

// private String model;

private String color;

private int horsePower;

private String engineType;

private String gearBoxType;

private String dateOfManufacture;

}


   public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    Car car1 = new Car("Mercedes", "slk", "blue", 199, "bnz", "r",

            "18 August 2005");

    Car car2 = new Car("peugeot", "206 cc", "blue", 165,"bnz", "r", "200");

    Car car3 = new Car("peugeot", "206" , "red", 155, "bnz", "r", "2005");


    Car[] carArray = new Car[3];

    carArray[0] = car1;

    carArray[1] = car2;

    carArray[2] = car3;


    System.out.println("this is the result from the 1st method:");

    System.out.println("enter brand:");

    char ch =  scan.next().charAt(0);

    searchCarByBrand(carArray,ch);


}


一只斗牛犬
浏览 240回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java