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

求助:实现基于堆叠(Stack)模式的每页3列数据排列的C#循环及数组索引计算

Understanding the Stack Pagination Logic & C# Implementation

First, let’s break down how this stack-based pagination works—once you get the core pattern, the code falls right into place:

  • Total Pages Calculation: The number of pages is the ceiling of totalItems / columnsPerPage. For example, 8 items with 3 columns → 3 pages, 22 items with 3 columns → 8 pages. We can compute this with integer math using (totalItems + columnsPerPage - 1) / columnsPerPage to avoid floating-point operations.

  • Index Calculation per Page: For each page p (0-based) and each column c (0-based), the formula for the item index is:

    index = c * totalPages + p
    

    If this index is less than the total number of items, it’s valid; otherwise, we use null as a placeholder.

Example Verification

Let’s confirm with your first example (8 items, 3 columns):

  • Total pages = (8 + 3 -1) /3 = 10/3 =3
  • Page 0: 0*3+0=0, 1*3+0=3, 2*3+0=6 → [0,3,6]
  • Page1: 0*3+1=1, 1*3+1=4, 2*3+1=7 → [1,4,7]
  • Page2: 0*3+2=2, 1*3+2=5, 2*3+2=8 (invalid) → [2,5,null]

Perfect match! For the 22-item example, total pages = (22+3-1)/3=24/3=8, which generates exactly the page indices you listed.

C# Code Implementation

Here’s a complete, tested method that generates all pages with the correct indices:

using System;
using System.Collections.Generic;
using System.Linq;

public class StackPagination
{
    public static List<List<int?>> GeneratePages(int totalItems, int columnsPerPage)
    {
        var pages = new List<List<int?>>();
        
        if (totalItems <= 0 || columnsPerPage <= 0)
            return pages;
            
        // Calculate total pages (ceiling of totalItems / columnsPerPage)
        int totalPages = (totalItems + columnsPerPage - 1) / columnsPerPage;
        
        for (int pageIndex = 0; pageIndex < totalPages; pageIndex++)
        {
            var currentPage = new List<int?>();
            
            for (int column = 0; column < columnsPerPage; column++)
            {
                int index = column * totalPages + pageIndex;
                currentPage.Add(index < totalItems ? index : null);
            }
            
            pages.Add(currentPage);
        }
        
        return pages;
    }
    
    // Test with your examples
    public static void Main()
    {
        // Example 1: 8 items, 3 columns
        Console.WriteLine("Example 1: 8 items, 3 columns");
        var pages1 = GeneratePages(8, 3);
        for (int i = 0; i < pages1.Count; i++)
        {
            var pageContent = string.Join(", ", pages1[i].Select(x => x.HasValue ? x.ToString() : "null"));
            Console.WriteLine($"第{i+1}页: {pageContent}");
        }
        
        // Example 2: 22 items, 3 columns
        Console.WriteLine("\nExample 2: 22 items, 3 columns");
        var pages2 = GeneratePages(22, 3);
        for (int i = 0; i < pages2.Count; i++)
        {
            var pageContent = string.Join(", ", pages2[i].Select(x => x.HasValue ? x.ToString() : "null"));
            Console.WriteLine($"第{i+1}页: {pageContent}");
        }
    }
}

Output

When you run this code, it will produce exactly the output from your examples:

Example 1: 8 items, 3 columns
第1页: 0, 3, 6
第2页: 1, 4, 7
第3页: 2, 5, null

Example 2: 22 items, 3 columns
第1页: 0, 8, 16
第2页: 1, 9, 17
第3页: 2, 10, 18
第4页: 3, 11, 19
第5页: 4, 12, 20
第6页: 5, 13, 21
第7页: 6, 14, null
第8页: 7, 15, null

This code is efficient, easy to modify, and perfectly aligns with the stack-based pagination logic you need.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 07:32:44