You need to enable JavaScript to run this app. Java ArrayList打印内存地址问题 及printAnimals、reserveAnimal实现咨询
优惠活动
大模型
产品
解决方案
定价
更多

Java ArrayList打印内存地址问题 及printAnimals、reserveAnimal实现咨询

Java救援动物管理系统开发问题
  • 打印ArrayList数组列表时,输出内容全部为对象的内存地址。目前已经跑通所有数据录入逻辑,但不清楚如何正确展示已录入的内容
  • 尚未完成printAnimals函数、reserveAnimal函数的开发

以下是当前项目使用的Java代码(代码结构保持不变,原有注释已翻译为中文):

import java.util.ArrayList;
import java.util.Scanner;

public class Driver {
    // 私有静态Scanner对象,用于接收用户输入
    private static Scanner scanner = new Scanner(System.in);

    private static ArrayList<Dog> dogList = new ArrayList<Dog>();

    private static ArrayList<Monkey> monkeyList = new ArrayList<Monkey>();

    public static void main(String[] args) {
        String menuChoice = "";
        
        initializeDogList();
        initializeMonkeyList();
        // 基于用户输入展示并执行菜单选择的循环
        while( !menuChoice.equalsIgnoreCase("q") ){
            displayMenu();
            menuChoice = scanner.nextLine();
            switch(menuChoice) {
                case "1":
                    intakeNewDog(scanner);
                    System.out.println("");
                    break;
                case "2":
                    intakeNewMonkey(scanner);
                    System.out.println("");
                    break;
                case "3":
                    reserveAnimal(scanner);
                    break;
                case "4":
                    System.out.println(dogList);
                    break;
                case "5":
                    System.out.println(monkeyList);
                    break;
                case "6":
                    printAnimals();
                    break;
            }
        }
    }

    // 该方法用于打印菜单选项
    public static void displayMenu() {
        System.out.println("\n\n");
        System.out.println("\t\t\t\tRescue Animal System Menu");
        System.out.println("[1] Intake a new dog");
        System.out.println("[2] Intake a new monkey");
        System.out.println("[3] Reserve an animal");
        System.out.println("[4] Print a list of all dogs");
        System.out.println("[5] Print a list of all monkeys");
        System.out.println("[6] Print a list of all animals that are not reserved");
        System.out.println("[q] Quit application");
        System.out.println();
        System.out.println("Enter a menu selection");
    }

    // 向列表中添加测试用Dog数据
    public static void initializeDogList() { 
        Dog dog1 = new Dog("Spot", "German Shepherd", "male", "1", "25.6", "05-12-2019", "United States", "intake", false, "United States");
        dogList.add(dog1);
    }

    // 向列表中添加测试用Monkey数据(测试可选)
    public static void initializeMonkeyList() {
        Monkey monkey1 = new Monkey("Jim", "12", "Male", "3", "12", "12-1-2005", "Venezuela", "Phase 7", false, "United States", "15", "5", "Capuchin");
        monkeyList.add(monkey1);
    }

    // 补全intakeNewDog方法,检查Dog是否已存在的输入校验逻辑已实现
    public static void intakeNewDog(Scanner scanner) {
        System.out.println("What is the dog's name?");
        String name = scanner.nextLine();
        for(Dog dog: dogList) {
            if(dog.getName().equalsIgnoreCase(name)) {
                System.out.println("\n\nThis dog is already in our system\n\n");
                return; // 返回菜单
            }
        }
        System.out.println("What is the dog's breed?");
        String breed = scanner.nextLine();
        
        System.out.println("What is the dog's gender?");
        String gender = scanner.nextLine();
        
        System.out.println("What is the dog's age?");
        String age = scanner.nextLine();
        
        System.out.println("What is the dog's weight?");
        String weight = scanner.nextLine();
        
        System.out.println("What is the dog's acquisition date?");
        String acquisitionDate = scanner.nextLine();
        
        System.out.println("What is the dog's acquisition country?");
        String acquisitionCountry = scanner.nextLine();
        
        System.out.println("what is the dog's training status?");
        String trainingStatus = scanner.nextLine();
        
        System.out.println("Is the dog reserved <true or false>?");
        boolean reservedBoolean = scanner.nextBoolean();
        
        System.out.println("What is the dog's service country?");
        String serviceCountry = scanner.nextLine();
        
        Dog dog = new Dog(name, breed, gender, age, weight, acquisitionDate, acquisitionCountry, trainingStatus, reservedBoolean, serviceCountry);
        dogList.add(dog);
    }

    // 执行Monkey录入的Scanner逻辑,根据用户输入添加数据
    public static void intakeNewMonkey(Scanner scanner) {
        System.out.println("What is the new monkey's name?");
        String name = scanner.nextLine();
        for (Monkey monkey: monkeyList) {
            if(monkey.getName().equalsIgnoreCase(name)) {
                System.out.println("\n\nThis dog is already in our system.\n\n");
                return;
            }
        }
        
        System.out.println("What is the monkey's tail length?");
        String tailLength = scanner.nextLine();
        
        System.out.println("What is the monkey's gender?");
        String gender = scanner.nextLine();
        
        System.out.println("What is the monkey's age?");
        String age = scanner.nextLine();
        
        System.out.println("What is the monkey's weight?");
        String weight = scanner.nextLine();
        
        System.out.println("What is the monkey's acquisition date?");
        String acquisitionDate = scanner.nextLine();
        
        System.out.println("What is the monkey's acquisition country?");
        String acquisitionCountry = scanner.nextLine();
        
        System.out.println("What is the monkey's training status?");
        String trainingStatus = scanner.nextLine();
        
        System.out.println("Is the monkey reserved? <enter true or false>");
        boolean reservedBoolean = scanner.nextBoolean();
        
        System.out.println("What is the monkey's service country?");
        String serviceCountry = scanner.nextLine();
        
        System.out.println("What is the monkey's body length?");
        String bodyLength = scanner.nextLine();
        
        System.out.println("What is the monkey's height?");
        String height = scanner.nextLine();
        
        System.out.println("What is the monkey's species?");
        String species = scanner.nextLine();
        
        Monkey monkey = new Monkey(name, tailLength, gender, age, weight, acquisitionDate, acquisitionCountry, trainingStatus, reservedBoolean, serviceCountry, bodyLength, height, species);
        monkeyList.add(monkey);
    }

    // 补全reserveAnimal方法,需根据动物类型和服务国家查找对应动物
    public static void reserveAnimal(Scanner scanner) {
    }

    // 补全printAnimals方法
    // 需包含动物名称、状态、获取国家及是否已预约的信息
    // 注意该方法对应三个不同的菜单项
    // printAnimals()方法根据listType参数有三种不同的输出
    // dog - 打印所有Dog列表
    // monkey - 打印所有Monkey列表
    // available - 合并打印所有完成训练(“in service”)但未预约的动物
    // 注意只需完整实现其中一个列表即可
    // 其余列表可打印“该选项待实现”的提示语句
    // 若要获得“优秀”评分,需正确实现“available”列表
    public static void printAnimals() {
    }
}

本内容的提问来源于stack exchange,提问作者Seth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.02 19:06:03