Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 划分列表<;字符串>;进入大小相同的子列表N_C#_List - Fatal编程技术网

C# 划分列表<;字符串>;进入大小相同的子列表N

C# 划分列表<;字符串>;进入大小相同的子列表N,c#,list,C#,List,我试图对字符串列表(大小为N)进行切片,并返回一个基于被切片为相等部分(X)的列表的范围 例如,如果我有一个10个元素的列表,我的层数是5 元素0和1是第1层。要素2和要素3为第2层。在方法的末尾,我返回参数中指定的层 我正在努力解决的是,如果列表计数不能被层数整除。例如,23/5=4.6。这意味着它们将是5套4套,然后剩下3套。我希望结果是5层5,5,5,5,3(最后一层只是剩余的元素数) 到目前为止,我已经包括了我的代码,但我真的被困在如何确保列表大小尽可能相等以及如何处理余数上 // Ge

我试图对字符串列表(大小为N)进行切片,并返回一个基于被切片为相等部分(X)的列表的范围

例如,如果我有一个10个元素的列表,我的层数是5

元素0和1是第1层。要素2和要素3为第2层。在方法的末尾,我返回参数中指定的层

我正在努力解决的是,如果列表计数不能被层数整除。例如,23/5=4.6。这意味着它们将是5套4套,然后剩下3套。我希望结果是5层5,5,5,5,3(最后一层只是剩余的元素数)

到目前为止,我已经包括了我的代码,但我真的被困在如何确保列表大小尽可能相等以及如何处理余数上

// Gets a list and returns a range by the tier specified
public List<string> GetRangeByTierIndex(List<string> listToDivide, int numOfTiers, int tierIndexToGet)
{
    int numOfElementsPerList = listToDivide.Count / numOfTiers;

    int index = (tierToGet - 1) * numOfElementsPerList;

    return listToDivide.GetRange(index, numOfElementsPerList);
}
//获取列表并返回指定层的范围
公共列表GetRangeByTierIndex(列表列表ToDivide、int-numOfTiers、int-tierIndexToGet)
{
int numOfElementsPerList=listToDivide.Count/numofiters;
int index=(tierToGet-1)*numOfElementsPerList;
返回listToDivide.GetRange(索引,numElementsPerList);
}

注意:忘了提一下,我也不能使用LINQ来解决这个问题(AOT和iOS问题)。

这个想法是使用模,它是
列表的除法的剩余部分。按
numofiers
计数。如果该余数大于零,则索引小于或等于该余数的所有层都将多包含一个元素。因此,每一层的起始索引也必须修正。请注意,我没有编写任何检查(例如,如果主列表中的元素数为零,
numofiers
,等等……但如果需要,您可以添加这些检查)。此外,以您的示例为例,这将给出包含
5,5,5,4,4
元素的列表,而不是
5,5,5,3
,但我认为这样更好。不管怎样,我希望它能满足你的需要。代码应该类似于:

public List<string> GetRangeByTierIndex(List<string> listToDivide, int numOfTiers, int tierIndexToGet)
{
    int remaining = listToDivide.Count % numOfTiers;
    int numOfElementsPerList = listToDivide.Count / numOfTiers;
    int index = (tierIndexToGet - 1) * numOfElementsPerList;
    if (remaining > 0)
    {
        // most increase position of index because of numOfElementsPerList correction bellow
        index += tierIndexToGet > remaining ? remaining : tierIndexToGet - 1;
        // first 'remaining-th' tiers will have +1 element
        numOfElementsPerList += tierIndexToGet <= remaining ? 1 : 0;
    }
    return listToDivide.GetRange(index, numOfElementsPerList);
}
public List GetRangeByTierIndex(List listToDivide、int numofiers、int tierIndexToGet)
{
剩余整数=listToDivide.Count%numOfTiers;
int numOfElementsPerList=listToDivide.Count/numofiters;
int索引=(tierIndexToGet-1)*numElementsPerList;
如果(剩余>0)
{
//由于NumoElementsPerlist修正波纹管,指数的大部分增加位置如下
索引+=tierIndexToGet>剩余?剩余:tierIndexToGet-1;
//第一个“剩余th”层将具有+1元素
NumoElementsPerList+=tierIndexToGet示例:23和5

  • 23/5=4//4层5-使用整数除法
  • 23%5=3//1层,共3层

看看这个:对不起,我之前应该提到我不能使用LINQ解决这个问题。这里也有一些非LINQ解决方案。我相信你的第一个10元素5层的示例是5组2,但是在你的第二个23元素5层的示例中,你说这是4组5。是不是应该是5组4?是的,应该是5组4:)此解决方案的问题是,如果主列表中有21个元素,则层中的元素数将为5,5,5,1。我的示例给出了5,4,4,4,这是更好的分布