列表到二维数组 [英] List to two-dimensional Array

查看:60
本文介绍了列表到二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将整数列表转换为二维数组?

How would one go about converting a list of integers to a two-dimensional array?

List<int> integerList = new List<int>();
integerList.Add(1);
integerList.Add(2);
...
integerList.Add(250000);
int[,] integerArray = new int[500,500];

//fill integerArray with integerList values here

目标输出应成行,从 0-499 填充 x,然后将 y 增加 1 并重复.integerArray[x,y]

Target output should be in rows, filling x from 0-499 then incrementing y by 1 and repeat. integerArray[x,y]

推荐答案

试试这个:

int i = 0;
foreach(var number in integerList)
{
    integerArray[i % 500, (int)(i / 500)] = number;
    i++;
}

如果你想让数字先在列中递增,只需将数组内部的 mod 和 div 操作转置即可.

If you want to the number to increment through the column first, just transpose the mod and div operations inside the array.

这篇关于列表到二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆