Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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# 超出范围的二维数组_C#_Arrays - Fatal编程技术网

C# 超出范围的二维数组

C# 超出范围的二维数组,c#,arrays,C#,Arrays,技术人员-- 当我有一个单一通道或当我有一个偶数分裂时,此代码工作。当我有一个余数时,它几乎可以工作…因为它将创建下一个通道,它将拉入项目,但随后超出范围。以下是问题的核心: items_per_batch = batchcount / (int)channels; // subsets = batch.Split(items_per_batch); 每批的items_实际上是用来给拆分扩展提供一个关于要拆分多少项的通用数字。如果它看到一个余数,它只会创建另一个子集数组。我真

技术人员-- 当我有一个单一通道或当我有一个偶数分裂时,此代码工作。当我有一个余数时,它几乎可以工作…因为它将创建下一个通道,它将拉入项目,但随后超出范围。以下是问题的核心:

    items_per_batch = batchcount / (int)channels; //
    subsets = batch.Split(items_per_batch); 
每批的items_实际上是用来给拆分扩展提供一个关于要拆分多少项的通用数字。如果它看到一个余数,它只会创建另一个子集数组。我真正需要做的是记录物品的长度。我试过:

    int idx2 = subset.GetLength(1) 
有一次,但是使用该值的forloop也超出了范围。有人有什么建议吗

    static void channelassign()
    {
        int THRESHOLD = 2;
        string[] batch = new string[]
        { "item1", "item2", "item3", "item4","item5","item6","item7" };
        int batchcount = batch.Count();
        int items_per_batch;
        string[][] subsets;
        int idx1;
        int idx2;


        if (THRESHOLD != 0) //avoid accidental division by 0.
        {

            float channels = batchcount / THRESHOLD;
            if (channels < 1)
            {
                channels = 1; // only 1 channel is needed
                items_per_batch = batchcount; // process all items
                idx1 = 1; // fix value to a single channel
                idx2 = (batchcount - 1); // true start of array is 0
                subsets = batch.Split(batchcount); //splits correctly
            }
            else
            {
              // decide how many items will be included per batch
              channels =  (int)Math.Round(channels, 
                  MidpointRounding.ToEven); //determines channel number
              items_per_batch = batchcount / (int)channels; //
              subsets = batch.Split(items_per_batch); 
              idx1 = subsets.GetLength(0); // gets channel# assigned by split
              // idx2 = subsets.GetLength(1); // gets items back from splits

            }

            //distribute contents of batch amongst channels


            for (int channel = 0; channel < idx1; channel++)
            {
                for (int i = 0; i < items_per_batch; i++)
                {
                    Console.WriteLine(" Channel:" + channel.ToString() + " 
                       ItemName: {0} ", subsets[channel][i]);
                }
            }


        }
        else
        {
            Console.WriteLine("Threshold value set to zero. This 
               is an invalid value. Please set THRESHOLD.");
        }
static void channelassign()
{
int阈值=2;
字符串[]批处理=新字符串[]
{“项目1”、“项目2”、“项目3”、“项目4”、“项目5”、“项目6”、“项目7”};
int batchcount=batch.Count();
每批整数项;
字符串[][]子集;
int-idx1;
int-idx2;
if(THRESHOLD!=0)//避免意外除以0。
{
浮动通道=批次计数/阈值;
中频(通道<1)
{
通道=1;//只需要一个通道
items\u per\u batch=batchcount;//处理所有项目
idx1=1;//将值固定到单个通道
idx2=(batchcount-1);//数组的真正开始是0
subsets=batch.Split(batchcount);//正确拆分
}
其他的
{
//决定每批包含多少项
通道=(int)数学圆(通道,
中点舍入.ToEven);//确定通道号
每批物料数量=批次计数/(整数)通道//
子集=批次分割(每个批次的项目);
idx1=subsets.GetLength(0);//获取split分配的通道
//idx2=subsets.GetLength(1);//从拆分中返回项
}
//在渠道之间分发批量内容
用于(int通道=0;通道
部门

float channels = batchcount / THRESHOLD;
int
s上执行,因此您的
float通道总是有一个整数值,等于

floor(batchcount / THRESHOLD)
但那不是你问题的原因,是吗

for (int channel = 0; channel < idx1; channel++)
{
    for (int i = 0; i < items_per_batch; i++)
for(int通道=0;通道

如果
batchcount
不是
channels
的倍数,则某些通道每批
项少于
项。因此,内部循环尝试访问
子集[channel][i]
这是不存在的。

我在你的代码中看到了不少于三种不同的变量命名约定。老实说,这有点精神分裂。不知道使用了什么拆分扩展方法,或者你提供了它的源代码;你没有提供任何东西让任何人继续下去……没错——我想我可以控制内部循环with:idx2的值从第二维度返回,但这不起作用。你对控制第二个循环有什么建议?
对于(int i=0;i
,我想?应该可以。我已经疯了!这是一个锯齿状的dd数组。idx2永远无法解决这个问题!是的,就是它!