0

在下面的代码中,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 会怎样?..或更大?

4

5 回答 5

7

使用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 });

编辑:

如何从列表中获取/设置值?

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]);
于 2011-01-18T02:28:34.190 回答
3

首先,请考虑使用Dictionary<DateTime, List<Float>>, 而不是数组。由于您的代码示例都涉及将数组大小扩大 1,这对我来说意味着您将多次调整数组的大小以达到其最终大小。如果是这样的话,那么选择一个可以自行扩展并且可以有效扩展的容器会更好。

其次,您所有的示例都使用的数组长度比字典中的项目数大一。这是一个不寻常的关系,你确定 aDictionary<DateTime, (some group of floats)>是正确的容器吗?

也就是说,这里是如何在字典中调整数组的大小。

SortedDictionary<DateTime, float[]> Pages = new SortedDictionary<DateTime,float[]>();
DateTime date = DateTime.UtcNow;

float[] arr = Pages[date];
Array.Resize<float>(ref arr, arr.Length + 1);
Pages[date] = arr; // [] overwrites the old value, unlike Add().
于 2011-01-18T02:32:03.550 回答
1

Array.Resize(ref tt, Pages.Count + 1); 页数[tempTime] = tt;

于 2011-01-18T02:30:01.350 回答
1

代替

public SortedDictionary< DateTime, float[]> Pages { get; 放; },

你可以使用

public SortedDictionary< DateTime, List< float>> Pages { get; 放; },

所以你不必调整大小,如果你仍然想将它作为一个数组访问,你总是可以使用 List 类的 ToArray() 。

希望这可以帮助

于 2011-01-18T02:30:34.287 回答
1

我会使用其他答案中建议的列表。您无法更改数组的原因Array.Resize()是它会将数组复制到一个新数组中,并为该数组返回(下面的反射器输出)的引用 - 所以除非您重新分配新值,否则您将Pages[tempTime]不走运。

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public static void Resize<T>(ref T[] array, int newSize)
{
    if (newSize < 0)
    {
        throw new ArgumentOutOfRangeException("newSize", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
    }
    T[] sourceArray = array;
    if (sourceArray == null)
    {
        array = new T[newSize];
    }
    else if (sourceArray.Length != newSize)
    {
        T[] destinationArray = new T[newSize];
        Copy(sourceArray, 0, destinationArray, 0, (sourceArray.Length > newSize) ? newSize : sourceArray.Length);
        array = destinationArray;
    }
}
于 2011-01-18T02:45:05.153 回答