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

实现2D数组指定列最大值下移存储至列底的问题求助

现有代码的问题

  • C# 中的int[,]多维数组属于定长结构,初始化后无法直接修改行数/列数,你现有代码中试图通过Array.Copy扩充数组的操作是不生效的
  • 最大值初始值设为0存在逻辑缺陷:如果目标列所有值都是负数,会错误识别最大值
  • Array.Copy的参数用法完全错误,多维数组的复制参数是线性索引,不能直接按行列偏移量填写
  • 不需要在遍历找最大值的同时做移动操作,可以先遍历一次拿到最大值,再做元素调整效率更高、逻辑更清晰

实现方案

以下默认优先满足最常见的需求:不改变原数组总尺寸,将指定列所有最大值移动到列的末尾位置,非最大值元素保持原有相对顺序。如果你的需求是保留原列所有元素、在列的底部新增行存储所有最大值,可以参考第二个实现方案。

方案1:不改变原数组尺寸,移动最大值到列底部

void StoreHighestValueAtBottom(int[,] matrix, int column)
{
    // 入参合法性校验
    if (matrix == null) throw new ArgumentNullException(nameof(matrix));
    int rowCount = matrix.GetLength(0);
    int colCount = matrix.GetLength(1);
    if (column < 0 || column >= colCount) throw new ArgumentOutOfRangeException(nameof(column));

    // 第一步:遍历找到目标列的最大值
    int maxValue = matrix[0, column];
    for (int row = 0; row < rowCount; row++)
    {
        if (matrix[row, column] > maxValue)
        {
            maxValue = matrix[row, column];
        }
    }

    // 第二步:提取所有非最大值,按原有顺序存到临时数组前半段
    int nonMaxIndex = 0;
    int[] tempCol = new int[rowCount];
    for (int row = 0; row < rowCount; row++)
    {
        if (matrix[row, column] != maxValue)
        {
            tempCol[nonMaxIndex++] = matrix[row, column];
        }
    }

    // 第三步:临时数组后半段填充最大值
    for (int i = nonMaxIndex; i < rowCount; i++)
    {
        tempCol[i] = maxValue;
    }

    // 第四步:把处理后的列数据写回原数组
    for (int row = 0; row < rowCount; row++)
    {
        matrix[row, column] = tempCol[row];
    }
}

方案2:新增行存储所有最大值,返回新的多维数组

如果需要保留原列所有元素,在列底部新增对应行数存储最大值,因原多维数组定长,需要返回新的数组:

int[,] StoreHighestValueAtBottom(int[,] matrix, int column)
{
    if (matrix == null) throw new ArgumentNullException(nameof(matrix));
    int oldRowCount = matrix.GetLength(0);
    int colCount = matrix.GetLength(1);
    if (column < 0 || column >= colCount) throw new ArgumentOutOfRangeException(nameof(column));

    // 同时统计最大值和最大值的个数
    int maxValue = matrix[0, column];
    int maxCount = 0;
    for (int row = 0; row < oldRowCount; row++)
    {
        if (matrix[row, column] > maxValue)
        {
            maxValue = matrix[row, column];
            maxCount = 1;
        }
        else if (matrix[row, column] == maxValue)
        {
            maxCount++;
        }
    }

    // 生成新的更大的多维数组
    int newRowCount = oldRowCount + maxCount;
    int[,] newMatrix = new int[newRowCount, colCount];

    // 复制原数组全部内容到新数组
    for (int row = 0; row < oldRowCount; row++)
    {
        for (int col = 0; col < colCount; col++)
        {
            newMatrix[row, col] = matrix[row, col];
        }
    }

    // 目标列底部填充最大值
    for (int i = 0; i < maxCount; i++)
    {
        newMatrix[oldRowCount + i, column] = maxValue;
    }

    return newMatrix;
}

测试用例参考

你可以用以下代码生成随机数组测试效果:

Random rnd = new Random();
// 生成5行3列的随机测试数组,取值范围1-9
int[,] testMatrix = new int[5, 3];
for (int row = 0; row < 5; row++)
{
    for (int col = 0; col < 3; col++)
    {
        testMatrix[row, col] = rnd.Next(1, 10);
    }
}
// 处理索引为1的列(第二列)
StoreHighestValueAtBottom(testMatrix, 1);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 23:45:01