Java宝典:数组与集合的神秘面纱——从新手到高手的进阶之路

数据结构,编程的灵魂

数据结构,是程序设计的基石,而数组与集合,则是这门艺术中最为璀璨的明珠。它们的存在,让数据的组织和管理变得有序而高效。本文将深入剖析这些概念,通过丰富示例与实战演练,带你领略数据结构的无穷魅力。

第一章:数组——数据的基石

数组是数据结构中最基本的形式,它提供了一种连续存储相同类型元素的方式。然而,它的灵活性有限,一旦声明,长度便不可改变。

示例代码:

int[] numbers = {1, 2, 3, 4, 5}; // 初始化数组
for (int i = 0; i < numbers.length; i++) {
    System.out.print(numbers[i] + " ");
}
// 输出: 1 2 3 4 5
第二章:List——动态数组的力量

List集合克服了数组长度固定的局限,提供了动态增长与缩减的能力。它是实现队列、栈等高级数据结构的基础。

示例代码:

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.remove(0); // 删除第一个元素
System.out.println(list); // 输出: [2]
第三章:Set——无序且不重复的集合

Set集合确保了元素的唯一性,非常适合用于去重和成员检测。不同的Set实现(如HashSet与TreeSet)具有各自的特点和优势。

示例代码:

Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
set.add(1); // 不会被添加
System.out.println(set); // 输出: [1, 2]
第四章:Map——键值对的完美匹配

Map集合通过键值对的形式存储数据,提供了一种快速查找和更新数据的方法。HashMap和TreeMap是两种常见的Map实现。

示例代码:

Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
System.out.println(map.get("One")); // 输出: 1
构建一个图书管理系统

为了加深理解,我们将构建一个简单的图书管理系统。系统将利用数组、List、Set和Map来存储和管理图书信息,包括添加、删除、搜索图书等功能。

实战任务:

  1. 使用List存储图书列表。
  2. 使用Set存储已借阅的图书。
  3. 实现图书的添加、删除和搜索功能。

首先,定义Book类来存储图书的信息:

public class Book {
    private String title;
    private String author;
    private int year;
    
    public Book(String title, String author, int year) {
        this.title = title;
        this.author = author;
        this.year = year;
    }
    
    // Getter 和 Setter 方法
    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }
    public String getAuthor() { return author; }
    public void setAuthor(String author) { this.author = author; }
    public int getYear() { return year; }
    public void setYear(int year) { this.year = year; }
    
    @Override
    public String toString() {
        return "Title: " + title + ", Author: " + author + ", Year: " + year;
    }
}

接下来,创建图书管理系统的主类:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class LibrarySystem {
    private ArrayList<Book> books;
    private HashMap<String, Book> bookMap;
    private HashSet<String> borrowedBooks;

    public LibrarySystem() {
        books = new ArrayList<>();
        bookMap = new HashMap<>();
        borrowedBooks = new HashSet<>();
    }

    // 添加图书
    public void addBook(Book book) {
        if (!bookMap.containsKey(book.getTitle())) {
            books.add(book);
            bookMap.put(book.getTitle(), book);
            System.out.println("Book added successfully.");
        } else {
            System.out.println("Book already exists in the library.");
        }
    }

    // 删除图书
    public void removeBook(String title) {
        if (bookMap.containsKey(title)) {
            if (!borrowedBooks.contains(title)) {
                books.remove(bookMap.get(title));
                bookMap.remove(title);
                System.out.println("Book removed successfully.");
            } else {
                System.out.println("Cannot remove book as it is currently borrowed.");
            }
        } else {
            System.out.println("Book not found in the library.");
        }
    }

    // 借阅图书
    public void borrowBook(String title) {
        if (bookMap.containsKey(title)) {
            if (!borrowedBooks.contains(title)) {
                borrowedBooks.add(title);
                System.out.println("Book borrowed successfully.");
            } else {
                System.out.println("Book is already borrowed.");
            }
        } else {
            System.out.println("Book not found in the library.");
        }
    }

    // 归还图书
    public void returnBook(String title) {
        if (borrowedBooks.contains(title)) {
            borrowedBooks.remove(title);
            System.out.println("Book returned successfully.");
        } else {
            System.out.println("Book was not borrowed.");
        }
    }

    // 查询图书
    public Book searchBook(String title) {
        if (bookMap.containsKey(title)) {
            return bookMap.get(title);
        } else {
            System.out.println("Book not found in the library.");
            return null;
        }
    }

    // 显示所有图书
    public void displayAllBooks() {
        for (Book book : books) {
            System.out.println(book);
        }
    }
}

最后,我们可以创建一个测试类来演示系统的工作流程:

public class LibraryTest {
    public static void main(String[] args) {
        LibrarySystem system = new LibrarySystem();
        
        system.addBook(new Book("Java", "张三", 2023));
        system.addBook(new Book("Python", "李四", 2022));
        system.addBook(new Book("C++", "王五", 2021));
        
        system.displayAllBooks();
        
        system.borrowBook("Java");
        system.returnBook("Java");
        
        system.removeBook("Python");
        
        system.displayAllBooks();
    }
}
  • 18
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鹿( ﹡ˆoˆ﹡ )

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值