在下面的代码中,Pages 定义为
public SortedDictionary<DateTime, float[]> Pages { get; set; }
我正在尝试动态增加这个数组的大小。谁能告诉如何增加最里面的浮动 [] 的大小?
var tt = currentContainer.Pages[dateTime];
Array.Resize<float>(ref tt, currentContainer.Pages.Count + 1);
失败 1
我尝试了以下代码并获取索引超出范围异常
SortedDictionary<DateTime, float[]> Pages = new SortedDictionary<DateTime,float[]>();
float[] xx = new float[1];
xx[0] = 1;
DateTime tempTime = DateTime.UtcNow;
Pages.Add(tempTime, xx);
var tt = Pages[tempTime];
Array.Resize<float>(ref tt, Pages.Count + 1);
Pages[tempTime][1] = 2;
失败 2
以下给出了编译时错误(属性、索引或动态成员不能用作 ref 值)
SortedDictionary<DateTime, float[]> Pages = new SortedDictionary<DateTime,float[]>();
float[] xx = new float[1];
xx[0] = 1;
DateTime tempTime = DateTime.UtcNow;
Pages.Add(tempTime, xx);
var tt = Pages[tempTime];
// The line below is different from Fail 1 above ... compile time error
Array.Resize<float>(ref Pages[tempTime], Pages.Count + 1);
Pages[tempTime][1] = 2;
问题
调整此数组大小的最有效答案是什么?
如果最终大小可能是 100-200 个浮点数或 700-900 个浮点数,答案会改变吗?
如果我将分配大小从 +1 更改为 +128 会怎样?..或更大?