如何扩展锯齿状数组
How extend a jagged array
我尝试用 Array.AddRange
扩展锯齿状数组,但没有成功。
我不知道为什么它不起作用,我也不例外,但我的数组范围没有改变。
这是我使用的代码:
public World( ushort[][][] worldMatrice)
{
// OldWith = 864
ushort OldWidth = (ushort)worldMatrice.GetLength(0);
// Extend the matrice to (1024) => only the first level [1024][][]
worldMatrice.ToList().Add(new ushort[1024- OldWidth][]);
// NewWidth = 864 , and should be 1024 ...
ushort NewWidth = worldMatrice.getLenght(0);
}
这个
worldMatrice.ToList()
将创建您的数组的副本,然后您什么都不做
https://msdn.microsoft.com/en-us/library/bb397694.aspx
Array.AddRange() 不会改变数组的维度,并且 Array.Length 将始终 return 数组可以容纳的最大元素数,而不是数组的总数里面有非空元素。
如果您要更改数组的维度,您可能需要将值从旧数组转移到具有您想要的维度的新数组。
int[] newArray = new int[1024];
Array.Copy(oldArray, newArray, oldArray.Length);
要获取数组中非空元素的数量,请使用类似
的方法
int count = array.Count(s => s != null);
您没有保存输出。
试试这个:
worldMatrice = worldMatrice.ToList().Add(new ushort[1024- OldWidth][]).ToArray();
我尝试用 Array.AddRange
扩展锯齿状数组,但没有成功。
我不知道为什么它不起作用,我也不例外,但我的数组范围没有改变。
这是我使用的代码:
public World( ushort[][][] worldMatrice)
{
// OldWith = 864
ushort OldWidth = (ushort)worldMatrice.GetLength(0);
// Extend the matrice to (1024) => only the first level [1024][][]
worldMatrice.ToList().Add(new ushort[1024- OldWidth][]);
// NewWidth = 864 , and should be 1024 ...
ushort NewWidth = worldMatrice.getLenght(0);
}
这个
worldMatrice.ToList()
将创建您的数组的副本,然后您什么都不做
https://msdn.microsoft.com/en-us/library/bb397694.aspx
Array.AddRange() 不会改变数组的维度,并且 Array.Length 将始终 return 数组可以容纳的最大元素数,而不是数组的总数里面有非空元素。
如果您要更改数组的维度,您可能需要将值从旧数组转移到具有您想要的维度的新数组。
int[] newArray = new int[1024];
Array.Copy(oldArray, newArray, oldArray.Length);
要获取数组中非空元素的数量,请使用类似
的方法int count = array.Count(s => s != null);
您没有保存输出。 试试这个:
worldMatrice = worldMatrice.ToList().Add(new ushort[1024- OldWidth][]).ToArray();