You need to enable JavaScript to run this app. 如何将Integer数组数字按类型规则重排后填充到6行10列JTable
优惠活动
大模型
产品
解决方案
定价
更多

如何将Integer数组数字按类型规则重排后填充到6行10列JTable

Java 实现代码
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;
import java.util.List;

public class NumberTableFill {
    // 数字分类枚举
    enum NumType {
        SMALL, BIG, P
    }

    public static void main(String[] args) {
        // 示例输入数组
        Integer[] num = {0,3,4,5,6,1,3,10,4,3,1,0,2,2,3,4,1,0,8,7,6,6,5,8,9,0,5,10,8};
        int rowCount = 6;
        int colCount = 10;

        // 1. 初始化表格二维数组,默认值为null对应空单元格
        Object[][] tableData = new Object[rowCount][colCount];

        // 2. 将原数组拆分为连续同类型的块
        List<List<Integer>> typeBlocks = splitToSameTypeBlocks(num);

        // 3. 按规则填充表格
        int currentCol = 0;
        int currentRow = 0;
        for (List<Integer> block : typeBlocks) {
            for (Integer n : block) {
                // 当前列填充满时切换到右侧新列
                if (currentRow >= rowCount) {
                    currentCol++;
                    currentRow = 0;
                    // 超过最大列数时提前终止,可根据需求调整为报错逻辑
                    if (currentCol >= colCount) break;
                }
                tableData[currentRow][currentCol] = n;
                currentRow++;
            }
            // 同类型块处理完成,强制切换到右侧新列
            currentCol++;
            currentRow = 0;
            if (currentCol >= colCount) break;
        }

        // 4. 构建JTable展示结果
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("数字填充结果");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // 列名可自定义,这里用1-10作为列标识
            String[] columnNames = new String[colCount];
            for (int i = 0; i < colCount; i++) {
                columnNames[i] = String.valueOf(i+1);
            }
            DefaultTableModel model = new DefaultTableModel(tableData, columnNames);
            JTable table = new JTable(model);
            table.setRowHeight(30);
            frame.add(new JScrollPane(table));
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }

    // 将数组拆分为连续同类型的块
    private static List<List<Integer>> splitToSameTypeBlocks(Integer[] num) {
        List<List<Integer>> blocks = new ArrayList<>();
        if (num == null || num.length == 0) return blocks;

        List<Integer> currentBlock = new ArrayList<>();
        NumType currentType = getType(num[0]);
        currentBlock.add(num[0]);

        for (int i = 1; i < num.length; i++) {
            NumType type = getType(num[i]);
            if (type == currentType) {
                currentBlock.add(num[i]);
            } else {
                blocks.add(currentBlock);
                currentBlock = new ArrayList<>();
                currentType = type;
                currentBlock.add(num[i]);
            }
        }
        // 加入最后一个未处理的块
        blocks.add(currentBlock);
        return blocks;
    }

    // 匹配要求判断数字所属分类
    private static NumType getType(int n) {
        if (n >=0 && n <5) return NumType.SMALL;
        else if (n >=5 && n <10) return NumType.BIG;
        else return NumType.P;
    }
}

规则对应实现说明

  • 分类逻辑:getType 方法完全匹配要求的三类划分规则,将数字归为Small、Big、P三类
  • 连续同类型块拆分:splitToSameTypeBlocks 方法遍历原数组,把连续相同类型的数字聚合为同一个块,遇到不同类型就生成新块
  • 填充逻辑:
    • 处理每个块时,从当前列的当前行开始从上到下填充,填满6行就自动切换到右侧相邻列继续填
    • 单个块处理完成后直接切换到下一列,原列剩余单元格留空,符合类型切换换列的要求
    • 适配6行10列的容量限制,超出部分会自动终止填充

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.13 14:39:01