goo*_*ate 0 c# arrays boxing resize c#-3.0
在下面的代码中,Pages被定义为
public SortedDictionary<DateTime, float[]> Pages { get; set; }
Run Code Online (Sandbox Code Playgroud)
我试图动态增加这个数组的大小.任何人都可以告诉如何增加最里面的浮点数[]?
var tt = currentContainer.Pages[dateTime];
Array.Resize<float>(ref tt, currentContainer.Pages.Count + 1);
Run Code Online (Sandbox Code Playgroud)
失败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;
Run Code Online (Sandbox Code Playgroud)
失败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;
Run Code Online (Sandbox Code Playgroud)
题
调整此数组大小的最佳性能是什么?
如果最终尺寸可能是100-200浮标或700-900浮标,答案是否会改变?
如果我将分配大小从+1更改为+128怎么办?..还是更大?
用List<T>,
例,
SortedDictionary<DateTime, List<float>> data;
data = new SortedDictionary<DateTime, List<float>>();
data.Add(DateTime.Now, new List<float>() { 11.4f, 322.3f, 33.5f });
Run Code Online (Sandbox Code Playgroud)
编辑:
如何从列表中获取/设置值?
List<float> a = new List<float>()
{
10.2f,20.3f
};
float v1 = a[0];
float v2 = a[1];
Console.WriteLine("{0} {1}", v1, v2);
a[0] = 90.40f;
Console.WriteLine("{0} {1}", a[0],a[1]);
Run Code Online (Sandbox Code Playgroud)