Java 实现一个链表

public class MyList {

    static class Node {// 节点类
        Object data;
        Node next;

        public Node(Object data) {// 构造方法,为data赋值
            this.data = data;
            this.next = null;
        }
    }

    Node head;

    public MyList() {
        head = null;// 链表的构造方法
    }

    public void clear() {// 清除链表
        head = null;
    }

    public void bianli()// 遍历
    {
        Node p = head;
        while (p != null) {
            System.out.print(p.data + " ");
            p = p.next;
        }
        System.out.println();
    }

    public boolean isEmpty()// 判断是否为空
    {
        return head == null;
    }

    public int size() {// 节点个数
        Node p = head;
        int sum = 0;
        while (p != null) {
            sum++;
            p = p.next;
        }
        return sum;
    }

    // 在指定位置插入元素,下标从0开始
    public void insert(Object d, int pos) {
        if (pos < 0 || pos > size()) {
            throw new RuntimeException("下标错误");
        }
        Node newNode = new Node(d);
        if (pos == 0) {
            newNode.next = head;
            head = newNode;
        } else if (pos >= size() - 1) {
            get(size() - 1).next = newNode;
        } else {
            newNode.next = get(pos);
            get(pos - 1).next = newNode;
        }
    }

    public Node get(int pos) {
        if (pos < 0 || pos > size()) {
            throw new RuntimeException("下标错误");
        }
        if (pos == 0)
            return head;
        Node p = head;
        for (int i = 0; i < pos; i++)
            p = p.next;
        return p;
    }

    public static void main(String[] args) {

        MyList list = new MyList();
        list.insert(10, 0);
        list.insert(20, 1);
        list.insert(30, 0);
        list.insert(40, 1);

        System.out.println(list.size());
        list.bianli();
        System.out.println(list.isEmpty());
        System.out.println(list.get(2).data);
        list.clear();
        System.out.println(list.isEmpty());
    }

}

这里写图片描述

### Spring框架概述 Spring 是一个开源的 Java 平台,旨在简化企业级应用程序开发。它提供了一种全面的基础架构支持,使开发者能够专注于业务逻辑而不是底层技术细节[^1]。 #### 什么是 Spring 框架? Spring 框架是一个分层架构,由多个模块组成,这些模块可以单独使用也可以组合在一起形成完整的解决方案。它的核心理念是通过 **控制反转(IoC)** 和 **面向切面编程(AOP)** 来提高代码的可维护性和灵活性[^2]。 #### Spring 的主要功能 以下是 Spring 框架的主要功能及其用途: 1. **轻量级容器** - Spring 提供了一个轻量级的容器来管理对象生命周期和依赖关系。这个容器被称为 **IoC 容器**,负责实例化、配置和组装对象[^3]。 2. **依赖注入(DI)** - 依赖注入是一种设计模式,用于将组件之间的依赖关系外部化并交由容器管理。这使得代码更加松散耦合,便于单元测试和扩展[^4]。 3. **面向切面编程(AOP)** - AOP 允许开发者定义横切关注点(如日志记录、事务管理),并将它们分离出来独立处理。这样可以使业务逻辑更清晰,减少重复代码[^5]。 4. **数据访问/集成** - Spring 支持多种持久层技术和数据库操作工具,比如 JDBC、Hibernate、MyBatis 等,并提供了统一异常层次结构以简化错误处理过程。 5. **MVC Web 开发** - Spring MVC 是一种基于模型-视图-控制器的设计模式,帮助构建灵活且易于维护的 Web 应用程序。 6. **事务管理** - 统一了不同资源类型的声明式事务管理和编程式事务管理机制,降低了复杂度。 7. **消息传递支持** - 包括 JMS(Java Message Service)、WebSocket 及其他实时通信协议的支持。 8. **测试支持** - 内置强大的 Mock 对象库以及与其他流行测试框架无缝集成的能力,有助于编写高质量的应用程序。 9. **微服务与云原生支持** - 结合 Spring Boot 和 Spring Cloud 技术栈,快速搭建现代化分布式系统和服务治理方案。 ```java // 示例:简单展示如何创建一个被 Spring IoC 容器管理的对象 @Component public class HelloWorld { public void sayHello() { System.out.println("Hello, World!"); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值