You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java中实现每个节点存储N个元素的类链表结构?

Java实现分组链表结构的方案

你描述的结构本质是开环链表(也叫展开式链表/Unrolled Linked List),核心是每个链表节点(也就是你说的「组」)存储固定数量的元素和下一个组的引用,Java中没有指针概念,用对象引用实现等价逻辑即可。


1. 核心实现代码

public class GroupLinkedList {
    // 内部类:定义「组」结构
    private static class Group {
        int[] elements; // 存储本组的整数元素
        int usedCount; // 本组已使用的元素槽位数量
        Group next; // 指向下一个组的引用,默认值为null

        // 构造方法:指定本组最多存储的元素数量
        public Group(int capacity) {
            this.elements = new int[capacity];
            this.usedCount = 0;
            this.next = null;
        }
    }

    private final int groupCapacity; // 每个组的固定容量
    private Group head; // 第一个组的引用
    private Group tail; // 最后一个组的引用,方便快速追加元素

    // 对外构造方法:传入每个组要存储的元素数量,比如你示例中的4
    public GroupLinkedList(int groupCapacity) {
        if (groupCapacity <= 0) {
            throw new IllegalArgumentException("组容量必须大于0");
        }
        this.groupCapacity = groupCapacity;
        this.head = null;
        this.tail = null;
    }

    /**
     * 向结构中追加单个整数
     * 当前组填满后会自动创建新组,并将前一个组的next引用指向新组
     */
    public void add(int num) {
        // 还没有任何组时,创建第一个组
        if (head == null) {
            head = new Group(groupCapacity);
            tail = head;
        }
        // 最后一个组已经填满,新建组并修改前一个组的next引用
        if (tail.usedCount == groupCapacity) {
            Group newGroup = new Group(groupCapacity);
            tail.next = newGroup;
            tail = newGroup;
        }
        // 写入当前元素
        tail.elements[tail.usedCount] = num;
        tail.usedCount++;
    }

    /**
     * 手动追加完整的组
     * 对应你需求里直接创建指定大小的组、修改前一个组next指针的场景
     */
    public void addFullGroup(int[] groupNums) {
        if (groupNums.length != groupCapacity) {
            throw new IllegalArgumentException("组元素数量必须等于初始化时设定的容量");
        }
        Group newGroup = new Group(groupCapacity);
        System.arraycopy(groupNums, 0, newGroup.elements, 0, groupCapacity);
        newGroup.usedCount = groupCapacity;
        if (head == null) {
            head = newGroup;
            tail = newGroup;
            return;
        }
        tail.next = newGroup;
        tail = newGroup;
    }

    // 遍历所有组输出内容,用于验证效果
    public void printAll() {
        Group current = head;
        int groupNo = 1;
        while (current != null) {
            System.out.printf("第%d组元素:", groupNo);
            for (int i = 0; i < current.usedCount; i++) {
                System.out.print(current.elements[i] + " ");
            }
            System.out.printf(" | 下一个组是否存在:%s%n", current.next != null ? "是" : "否");
            current = current.next;
            groupNo++;
        }
    }
}

2. 使用示例

public class Test {
    public static void main(String[] args) {
        // 初始化每个组容量为4的结构
        GroupLinkedList list = new GroupLinkedList(4);
        
        // 方式1:逐个添加元素,自动分组
        for (int i = 1; i <= 4; i++) {
            list.add(i);
        }
        System.out.println("添加完第一个组后的输出:");
        list.printAll();

        // 方式2:手动添加完整的第二个组
        int[] secondGroup = {5,6,7,8};
        list.addFullGroup(secondGroup);
        System.out.println("\n添加完第二个组后的输出:");
        list.printAll();
    }
}

输出结果

添加完第一个组后的输出:
第1组元素:1 2 3 4 | 下一个组是否存在:否

添加完第二个组后的输出:
第1组元素:1 2 3 4 | 下一个组是否存在:是
第2组元素:5 6 7 8 | 下一个组是否存在:否


3. 关键逻辑说明

  • 每个组的容量在初始化GroupLinkedList时固定,你可以根据需求传入任意正整数作为组容量
  • next引用默认值为null,只有新建后续组时才会修改前一个组的next指向新组,完全匹配你需求的逻辑
  • 提供了两种添加数据的方式:逐个元素自动分组、手动批量添加整组,可根据实际场景选择

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 07:24:07