17370845950

Java实现简易通讯录查询_ArrayList基础应用项目
答案:通过定义Contact类和使用ArrayList实现通讯录的增删查功能,掌握Java面向对象与集合操作。

用Java实现一个简易通讯录,能添加、查询、删除联系人,是掌握ArrayList基础操作的典型项目。通过这个小练习,可以熟悉对象封装、集合存储和基本控制流程。

1. 定义联系人类(Contact)

每个联系人包含姓名和电话号码,封装成一个类便于管理。


class Contact {
    private String name;
    private String phone;
public Contact(String name, String phone) {
    this.name = name;
    this.phone = phone;
}

public String getName() {
    return name;
}

public String getPhone() {
    return phone;
}

@Override
public String toString() {
    return "姓名:" + name + ",电话:" + phone;
}

}

2. 使用ArrayList存储联系人

在主程序中创建ArrayList对象,用于动态保存多个Contact实例。

  • 导入java.util.ArrayListjava.util.Scanner
  • 声明ArrayList contacts = new ArrayList();
  • 通过Scanner接收用户输入

3. 实现基本功能菜单

提供交互式选项,支持增删查操作。


import java.util.*;

public class AddressBook { public static void main(String[] args) { ArrayList contacts = new ArrayList<>(); Scanner sc = new Scanner(System.in); int choice;

    while (true) {
        System.out.println("\n--- 简易通讯录 ---");
        System.out.println("1. 添加联系人");
        System.out.println("2. 查看所有联系人");
        System.out.println("3. 按姓名查找");
        System.out.println("4. 删除联系人");
        System.out.println("0. 退出");
        System.out.print("请选择操作:");
        choice = sc.nextInt();
        sc.nextLine(); // 吞掉换行符

        switch (choice) {
            case 1:
                System.out.print("输入姓名:");
                String name = sc.nextLine();
                System.out.print("输入电话:");
                String phone = sc.nextLine();
                contacts.add(new Contact(name, phone));
                System.out.println("添加成功!");
                break;

            case 2:
                if (contacts.isEmpty()) {
                    System.out.println("通讯录为空!");
                } else {
                    for (Contact c : contacts) {
                        System.out.println(c);
                    }
                }
                break;

            case 3:
                System.out.print("请输入要查找的姓名:");
                String searchName = sc.nextLine();
                boolean found = false;
                for (Contact c : contacts) {
                    if (c.getName().equals(searchName)) {
                        System.out.println("找到:" + c);
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    System.out.println("未找到该联系人。");
                }
                break;

            case 4:
                System.out.print("请输入要删除的姓名:");
                String delName = sc.nextLine();
                boolean removed = false;
                for (int i = 0; i zuojiankuohaophpcn contacts.size(); i++) {
                    if (contacts.get(i).getName().equals(delName)) {
                        contacts.remove(i);
                        System.out.println("删除成功!");
                        removed = true;
                        break;
                    }
                }
                if (!removed) {
                    System.out.println("未找到该联系人,无法删除。");
                }
                break;

            case 0:
                System.out.println("再见!");
                sc.close();
                return;

            default:
                System.out.println("无效选择,请重试。");
        }
    }
}

}

4. 关键点说明与改进建议

这个项目虽简单,但涵盖了面向对象和集合的核心思想。

  • ArrayList优势:自动扩容,方便增删查,适合不确定数量的数据存储
  • equals比较:使用name.equals()而非==,避免字符串引用误判
  • Scanner陷阱:nextInt()后紧跟nextLine()需额外调用一次nextLine()吸收回车
  • 可扩展方向:加入修改功能、按电话查找、保存到文件等

基本上就这些。掌握了这个例子,你就已经会用ArrayList处理实际问题了,不复杂但容易忽略细节。多敲几遍,理解每一步的作用,对后续学习很有帮助。