调整数组大小以丢失空值
Resizing array to lose empty values
我做了一个循环,从当前年龄到 x 年,比如 80 岁,每个月都循环。
我有一个 yearCalculation years 数组,每个 yearCalculation 都包含一个 monthCalculation 数组。 (以防万一有人想对列表发表评论,我目前正在使用数组,想看看是否有简单的解决方案。)
看起来如下:
yearCalculations[] years = years.InstantiateArray(//Number of years, [80 minus age]//);
monthCalculations[] months = months.InstantiateArray(//Number of months in a year, [this should be 12]//);
在实例化之后,我循环遍历所有周期并用各种计算填充它们。 (但是,在达到 x 岁之后,所有计算结果都将为零):
for (int i = 0; i < yearCalculations.Length; i++) {
for (int j = 0; j < yearCalculations[i].monthCalculations.Length; j++) {
Double age = calculateAge(birthDate, dateAtTimeX);
if(age < ageX){
//Do all sorts of calculations.
}else{
//Break out of the loops
}
}
}
正如您在 X (80) 岁时所了解的那样,计算将完成,但去年的计算将包含一些结果,而无需进行计算。可以说这是从第 7 个月开始的。调整此数组大小最简单的方法是什么,无需计算即可删除所有月份(因此索引 6 及以上)?
为了完整起见,这里是instantiateArray函数;
public static T[] InstantiateArray<T>(this T[] t, Int64 periods) where T : new()
{
t = new T[periods];
for (int i = 0; i < t.Length; i++){
t[i] = new T();
}
return t;
}
Array.Resize 方法应该可以解决新总长度的问题。你知道总的新长度是总的旧长度 - (12 - month as an int in year)
要从数组中删除空白值,您可以使用 LINQ
var arr = years.Where(x => !string.IsNullOrEmpty(x)).ToArray();//or what ever you need
您不能调整数组的大小。
The number of dimensions and the length of each dimension are established when the array instance is created. These values can't be changed during the lifetime of the instance.
像Array.Resize这样的方法实际上做的是分配一个新数组并将元素复制过来。理解这一点很重要。您不是在调整数组的大小,而是在重新分配它。
只要您使用数组,最终答案将归结为 "allocate a new array, then copy what you want to keep over to it"。
我做了一个循环,从当前年龄到 x 年,比如 80 岁,每个月都循环。
我有一个 yearCalculation years 数组,每个 yearCalculation 都包含一个 monthCalculation 数组。 (以防万一有人想对列表发表评论,我目前正在使用数组,想看看是否有简单的解决方案。)
看起来如下:
yearCalculations[] years = years.InstantiateArray(//Number of years, [80 minus age]//);
monthCalculations[] months = months.InstantiateArray(//Number of months in a year, [this should be 12]//);
在实例化之后,我循环遍历所有周期并用各种计算填充它们。 (但是,在达到 x 岁之后,所有计算结果都将为零):
for (int i = 0; i < yearCalculations.Length; i++) {
for (int j = 0; j < yearCalculations[i].monthCalculations.Length; j++) {
Double age = calculateAge(birthDate, dateAtTimeX);
if(age < ageX){
//Do all sorts of calculations.
}else{
//Break out of the loops
}
}
}
正如您在 X (80) 岁时所了解的那样,计算将完成,但去年的计算将包含一些结果,而无需进行计算。可以说这是从第 7 个月开始的。调整此数组大小最简单的方法是什么,无需计算即可删除所有月份(因此索引 6 及以上)?
为了完整起见,这里是instantiateArray函数;
public static T[] InstantiateArray<T>(this T[] t, Int64 periods) where T : new()
{
t = new T[periods];
for (int i = 0; i < t.Length; i++){
t[i] = new T();
}
return t;
}
Array.Resize 方法应该可以解决新总长度的问题。你知道总的新长度是总的旧长度 - (12 - month as an int in year)
要从数组中删除空白值,您可以使用 LINQ
var arr = years.Where(x => !string.IsNullOrEmpty(x)).ToArray();//or what ever you need
您不能调整数组的大小。
The number of dimensions and the length of each dimension are established when the array instance is created. These values can't be changed during the lifetime of the instance.
像Array.Resize这样的方法实际上做的是分配一个新数组并将元素复制过来。理解这一点很重要。您不是在调整数组的大小,而是在重新分配它。
只要您使用数组,最终答案将归结为 "allocate a new array, then copy what you want to keep over to it"。