我有一个大小为200的一维String
数组,偶数索引单元格(0,2,4,6,...)由Int
和奇数索引单元格填充(1,3, 5,7,...)由Float
填充。为了向这个数组添加一些值,我想把这个数组转移到大小为[100,2]的二维String
数组,第一行由Int
填充,第二行是由浮子填充。
问题是在数组之间传输时,我随机错过了一些值(我得到了Null)。我已经检查了数据,以确保我的数据格式正确。
int shomar = 0;
for (int y = 0; y < MITNeighborSource.Length; y +=2)
{
for (int x = 0; x < Cal.GetLength(0); x++)
{
if (MITNeighborSource[y] == Cal[x, 0])
{
MITNeighborSourceEdge[0, shomar] = MITNeighborSource[y]; //course ID
MITNeighborSourceEdge[1, shomar] = Cal[x, 2]; //output degree
MITNeighborSourceEdge[2, shomar] = Cal[x, 3]; //Input degree
MITNeighborSourceEdge[3, shomar] = MITNeighborSource[y+1]; //The cosine similarity
break;
}
if (MITNeighborSource[y] == Cal[x, 1])
{
MITNeighborSourceEdge[0, shomar] = MITNeighborSource[y]; //course ID
MITNeighborSourceEdge[1, shomar] = Cal[x, 4]; //output degree
MITNeighborSourceEdge[2, shomar] = Cal[x, 5]; //Input degree
MITNeighborSourceEdge[3, shomar] = MITNeighborSource[y+1]; //The cosine similarity
break;
}
}
shomar++;
}
可能是什么问题?
答案 0 :(得分:0)
如果您只想根据项目索引将1-D阵列分成2-D阵列,您可以像下面这样做。假设strArray是你的1-D数组:
string[] oneDArray = new[] {"200", "2.1", "40", "56.3", "34"};
var list = oneDArray .ToList();
var twoDArray = list.GroupBy(x => list.IndexOf(x)%2).Select(x=>x.ToArray()).ToArray();
这里两个DArray将有两个数组,包含来自oneDArray的偶数和奇数索引元素。