我的任务是生成数组的所有可能索引组合,其中数组可能是具有不同元素数量的单个 2D、3D、4D ... nD 数组。目前,我只能支持使用嵌套 for 循环的单个、2D 和 3D 数组。
例子:
byte[,,] sampleArray = new byte[5,4,3];
Generated Index Combinations:
sampleArray[0,0,0]
sampleArray[0,0,1]
sampleArray[0,0,2]
sampleArray[0,1,0]
sampleArray[0,1,1]
sampleArray[0,1,2]
sampleArray[0,2,0]
sampleArray[0,2,1]
sampleArray[0,2,2]
sampleArray[0,3,0]
sampleArray[0,3,1]
sampleArray[0,3,2]
sampleArray[1,0,0]
sampleArray[1,0,1]
sampleArray[1,0,2]
sampleArray[1,1,0]
sampleArray[1,1,1]
sampleArray[1,1,2]
sampleArray[1,2,0]
sampleArray[1,2,1]
sampleArray[1,2,2]
sampleArray[1,3,0]
sampleArray[1,3,1]
sampleArray[1,3,2]
sampleArray[2,0,0]
sampleArray[2,0,1]
sampleArray[2,0,2]
sampleArray[2,1,0]
sampleArray[2,1,1]
sampleArray[2,1,2]
sampleArray[2,2,0]
sampleArray[2,2,1]
sampleArray[2,2,2]
sampleArray[2,3,0]
sampleArray[2,3,1]
sampleArray[2,3,2]
sampleArray[3,0,0]
sampleArray[3,0,1]
sampleArray[3,0,2]
sampleArray[3,1,0]
sampleArray[3,1,1]
sampleArray[3,1,2]
sampleArray[3,2,0]
sampleArray[3,2,1]
sampleArray[3,2,2]
sampleArray[3,3,0]
sampleArray[3,3,1]
sampleArray[3,3,2]
sampleArray[4,0,0]
sampleArray[4,0,1]
sampleArray[4,0,2]
sampleArray[4,1,0]
sampleArray[4,1,1]
sampleArray[4,1,2]
sampleArray[4,2,0]
sampleArray[4,2,1]
sampleArray[4,2,2]
sampleArray[4,3,0]
sampleArray[4,3,1]
sampleArray[4,3,2]
我的代码:
protected void GenerateIndexCombinations(int indexCounter,
ref List<List<int>> indexList, Array arr, ref List<int> index)
{
int dimSize1 = arr.GetLength(0);
int dimSize2 = 0;
int dimSize3 = 0;
if (indexCounter > 1)
{
dimSize2 = arr.GetLength(1);
if (indexCounter > 2)
{
dimSize3 = arr.GetLength(2);
}
}
for (int a = 0; a < dimSize1; a++)
{
if (dimSize2 > 0)
{
for (int b = 0; b < dimSize2; b++)
{
if (dimSize3 > 0)
{
for (int c = 0; c < dimSize3; c++)
{
index = new List<int>();
index.Add(a);
index.Add(b);
index.Add(c);
indexList.Add(index);
}
}
else
{
index = new List<int>();
index.Add(a);
index.Add(b);
indexList.Add(index);
}
}
}
else
{
index = new List<int>();
index.Add(a);
indexList.Add(index);
}
}
}
其中:
int indexCounter 是维数。
数组 arr 是使用反射访问的数组:
Array arr = fieldInfoArray[j].GetValue(_classInstance) as Array;
ref List<List<int>> indexList
将是组合的容器。
ref List<int> index
是添加到 indexList 的个人编号。
由于维度大小以及每个维度的元素数量不固定,我想动态生成组合以替换我的嵌套 for 循环,有没有办法做到这一点?
感谢您的回答。