登录
首页 >  文章 >  java教程

Java实现简易通讯录查询系统

时间:2025-11-28 10:33:45 218浏览 收藏

积累知识,胜过积蓄金银!毕竟在文章开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《Java实现简易通讯录查询项目》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

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

Java实现简易通讯录查询_ArrayList基础应用项目

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

1. 定义联系人类(Contact)

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

<font face="Courier New">
class Contact {
    private String name;
    private String phone;
<pre class="brush:java;toolbar:false;">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. 实现基本功能菜单

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

<font face="Courier New">
import java.util.*;
<p>public class AddressBook {
public static void main(String[] args) {
ArrayList<Contact> contacts = new ArrayList<>();
Scanner sc = new Scanner(System.in);
int choice;</p><pre class="brush:java;toolbar:false;">    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 &lt; 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处理实际问题了,不复杂但容易忽略细节。多敲几遍,理解每一步的作用,对后续学习很有帮助。

好了,本文到此结束,带大家了解了《Java实现简易通讯录查询系统》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多文章知识!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>