【问题标题】:List Array Objects and their Variables to the Main method将数组对象及其变量列出到 Main 方法
【发布时间】:2019-04-21 03:17:17
【问题描述】:

好的,伙计们,我是新来的,但在无数不同的场合使用该网站试图找到解决我遇到的问题的方法。我有一个正在为学校工作的汽车库存计划。本质上,我使用 setter 和 getter 将对象添加到我的汽车类中的 ArrayList。

我在编写 listAllVehicles 方法时遇到了很多麻烦。起初我可以在车辆数组列表中列出车辆,但它会给我一个指向该对象的指针。所以它看起来像 com.automobile@1662365。我想在汽车类中创建一个名为 listAllVehicles 的循环方法,该方法循环遍历数组列表并打印每个车辆的简短描述,直到列表完成。我在编写代码方面取得了一些进展,但它不起作用并且 for 循环没有初始化。我相信你们会知道我做错了什么。

我的代码目前没有设置为 ToString 方法来格式化打印的内容,但我对这个想法持开放态度(已经尝试但无法弄清楚)

任何帮助将不胜感激。谢谢大家!

我试图转换为字符串。我搞砸了在 for 循环中强制转换为一个对象。确实有很多不同的东西,但我似乎无法让这一切正常工作。

public static void main(String[] args) {
        String model, make, color;
        int year = 0;
        int mileage = 0;
        Scanner userInput = new Scanner(System.in);

        System.out.println("[1]Add a Vehicle\n[2]List All 
        Vehicles\n[3]Update a Vehicle\n[4]Delete a Vehicle\n[5]Quit\n");
        System.out.print("Please choose from an option above: ");
        int userChoice = userInput.nextInt();
        switch(userChoice) {
        case 1:
            Scanner addVehicleScanner = new Scanner(System.in);
            Automobile newVehicle = new Automobile();
            System.out.print("What is the vehicle make: ");
            make = addVehicleScanner.next();
            newVehicle.setMake(make);
            System.out.print("What is the vehicle model: ");
            model = addVehicleScanner.next();
            newVehicle.setModel(model);
            System.out.print("What is the vehicle color: ");
            color = addVehicleScanner.next();
            newVehicle.setColor(color);
            System.out.print("What is the vehicle year: ");
            year = addVehicleScanner.nextInt();
            newVehicle.setYear(year);
            System.out.print("What is the vehicle mileage: ");
            mileage = addVehicleScanner.nextInt();
            newVehicle.setMileage(mileage);
            newVehicle.addVehicle(newVehicle);
            vehicleInventory.main(args);
            break;
        case 2:
            Automobile printAllVehicles = new Automobile();
            printAllVehicles.listAllVehicles();
            break;
        case 3:
            //FIX ME: Add Update Vehicle Code
            break;
        case 4:
            //FIX ME: Add Delete Vehicle Code
            break;
        case 5:
            System.out.println("\n\nGoodbye!");
            break;




public void listAllVehicles() {
        Automobile temp = new Automobile();
        for (int i = 0; i < vehicles.size(); i++) {
            temp = (Automobile) vehicles.get(i);
            System.out.println(temp);
        }
    }

【问题讨论】:

    标签: java arrays object arraylist


    【解决方案1】:

    让你的代码工作的一些建议

    1) 覆盖Automobile 类的toString 方法并返回汽车对象的完整详细信息,这样当您在Automobile 的对象上调用System.out.println 时,它会为您提供详细信息

    @Override
    public String toString() {
    return "Automobile [ model "+ model +"]";
    }
    

    2) 永远不要像vehicleInventory.main(args) 那样调用main 方法。 Main 方法将由 JVM 调用,作为类的起点。在您的代码中,您可以使用 while 循环而不是调用它

    【讨论】:

    • 你必须原谅我。我只有几周的时间来学习 Java。我将把这个覆盖代码放在哪里?几天前我尝试过,但没有正确实现它,或者在正确的地方,因为我无法让它工作......
    • Automobile 类中。谷歌如何在课堂上覆盖toString。如果您使用的是 IDE,您将有编写该方法的快捷方式(对于 eclipse,它是 Alt + Shift + S + S)
    • 好吧,我创建了覆盖代码。如下。 @Override public String toString() { return "汽车 [make=" + make + ", model=" + model + ", color=" + color + ", year=" + year + ", mileage=" + mileage + "]"; }
    • 那么我如何从主类调用这个方法来列出所有车辆。我假设一个 for 循环,你提到了一个 while 循环。再次,非常抱歉,还在学习中......
    • 当您使用System.out.println 打印Automobile 类的对象时,就像您在listAllVehicles 方法中所做的那样,该方法将被自动调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 2015-06-23
    相关资源
    最近更新 更多