本文介绍了路垫一个数组,以避免阵列误差范围之外的指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在我的列表中至少有183项,当我查询它,但有时在我的项目结果提取结果比数较低183.我现在据说修复垫,该计数小于的情况下阵183.

I expect to have at least 183 items in my list when I query it, but sometimes the result from my extract results in items count lower than 183. My current fix supposedly pads the array in the case that the count is less than 183.

if (extractArray.Count() < 183) {
    int arraysize= extractArray.Count();
    var tempArr = new String[183 - arraysize];
    List<string> itemsList = extractArray.ToList<string>();
    itemsList.AddRange(tempArr);
    var values = itemsList.ToArray();
    //-- Process the new array that is now at least 183 in length
}

但似乎我的解决方案是不是最好的。我想AP preciate任何其他的解决方案,可以帮助确保我获得至少183项每当发生摘录请。

But it seems my solution is not the best. I would appreciate any other solutions that could help ensure I get at least 183 items whenever the extract happens please.

推荐答案

该阵列基类实现的方法

if(extractArray.Length < 183)
    Array.Resize<string>(ref extractArray, 183);

但是,请记住,调整大小对性能有问题,因此只有当你需要某种原因阵列这种方法是非常有用的。如果你可以切换到一个List

However, keep in mind that resizing is problematic for performance, thus this method is useful only if you require the array for some reason. If you can switch to a List

和,我想你这里有一个字符串数组一维,所以我用length属性来检查数组中的有效数量的项目。

And, I suppose you have an unidimensional array of strings here, so I use the Length property to check the effective number of items in the array.

这篇关于路垫一个数组,以避免阵列误差范围之外的指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 05:37