将此 ArrayList 转换为通用列表是否有效?

发布于 2024-09-05 04:32:44 字数 301 浏览 15 评论 0原文

我正在编写的代码从非托管代码接收一个 ArrayList,并且此 ArrayList 将始终包含一个或多个 Grid_Heading_Blk 类型的对象。我考虑过将此 ArrayList 更改为通用列表,但我不确定转换操作是否会如此昂贵,以至于抵消使用通用列表的好处。目前,我只是在将 ArrayList 传递给将使用它的类之后运行一个 foreach (Grid_Heading_Blk in myArrayList) 操作来处理 ArrayList 内容。

我应该将 ArrayList 转换为通用类型列表吗?如果是这样,最有效的方法是什么?

The code I'm writing receives an ArrayList from unmanaged code, and this ArrayList will always contain one or more objects of type Grid_Heading_Blk. I've considered changing this ArrayList to a generic List, but I'm unsure if the conversion operation will be so expensive as to nullify the benefits of working with the generic list. Currently, I'm just running a foreach (Grid_Heading_Blk in myArrayList) operation to work with the ArrayList contents after passing the ArrayList to the class that will use it.

Should I convert the ArrayList to a generic typed list? And if so, what is the most efficient way of doing so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

菩提树下叶撕阳。 2024-09-12 04:32:44

下面介绍了一种从 ArrayList 创建通用列表的高性能方法。

List<Grid_Heading_Blk> myList = new List<Grid_Heading_Blk>(source.Count);
myList.AddRange(source.OfType<Grid_Heading_Blk>());

通过调用接受 int 的构造函数,后备存储仅分配一次。

与往常一样,您应该使用您通常使用的任何工具来测量性能。

Here's a stab at a performant way to create a generic list from an ArrayList.

List<Grid_Heading_Blk> myList = new List<Grid_Heading_Blk>(source.Count);
myList.AddRange(source.OfType<Grid_Heading_Blk>());

By calling the constructor that accepts an int, the backing storage is allocated only once.

As always, you should measure the performance using whatever tools you normally use.

爱的那么颓废 2024-09-12 04:32:44

我经常使用此清单来评估像您这样的问题:

  1. 使其正确
  2. 使其清晰
  3. 使其简洁
  4. 使其高效

List更能揭示意图>数组列表。因此,即使不考虑效率,第 2 项也已经取得了巨大胜利。

要将 ArrayList 转换为 List<>,您必须迭代 >ArrayList 一次并转换每个元素。 foreach 正在进行隐式转换,因此开销仅存在于额外的迭代中。

迭代序列两次将性能从 O(n) 降低到 O(2n),仍然是 O(n)(数量级,而不是值,这对性能来说很重要)。因此,您可以认为这种变化是良性的。

但是,如果实际上您所做的所有都是运行foreach,那么您应该直接使用ArrayList - 将其更改为List<> ; 不会给你带来更多的表达能力。

I often use this checklist to evaluate questions like yours:

  1. Make it correct
  2. Make it clear
  3. Make it concise
  4. Make it efficient

List<Grid_Heading_Blk> is far more intention-revealing than ArrayList. So, without even considering efficiency, there is already a big win for item 2.

To convert an ArrayList to a List<>, you have to iterate over the ArrayList once and cast each element. The foreach is doing an implicit cast, so the overhead is only in the extra iteration.

Iterating a sequence twice takes the performance from O(n) to O(2n), which is still O(n) (magnitude, not value, is what matters for performance). Therefore, you can consider the change benign.

However, if literally all you are doing is running the foreach, you should just use ArrayList directly - changing it to List<> buys you no more expressive power.

豆芽 2024-09-12 04:32:44

为什么需要转换ArrayList?老实说,您的 foreach 循环似乎可以解决问题。是的,正如凯文上面所说,您要支付的唯一惩罚是拆箱 - 但就目前情况而言,这是非常简单的代码,并且您可能没有足够的网格标题来支付真正的性能损失。

但如果你必须转换它,我想说,与其编写自己的 for 循环来转换为 List 泛型类型,不如使用采用 IEnumerable 类型的构造函数(某种 ArrayList 应该已经实现了。)

List<Grid_Heading_Blk> heading = new List<Grid_Heading_Blk>( arrayList );

Why do you need to convert the ArrayList at all? To be honest, your foreach loop seems like it would do the trick. Yes, as Kevin says above the only penalty you'd be paying is unboxing - but as it stands it is pretty simple code and you probably don't have enough grid headings to pay a real performance hit.

But if you must convert it I would say, rather than writing your own for loop to convert to the List generic type, it might be better to use the constructor which takes IEnumerable type (something ArrayList should implement already.)

List<Grid_Heading_Blk> heading = new List<Grid_Heading_Blk>( arrayList );
洋洋洒洒 2024-09-12 04:32:44

使用 ArrayList 的最大惩罚是拳击。

使用泛型你会得到:
1. 编译时安全
2. 泛型扩展
3. 消除将列表中的所有内容转换为对象类型的限制。

这些是您使用它们的优势。它们是有优势的,但如果您必须从 ArrayList 重新填充泛型,则可能不值得这样做,特别是如果您只是循环访问列表来获取对象。

The biggest penalty you have using ArrayLists is boxing.

With generics you get:
1. compile time safety
2. generics extensions
3. remove this limitation of having everything in the list convert to type object.

Those are the advantages you get to using them. They're advantage, but if you have to re-populate the generic from the ArrayList, it may not be worth doing, especially if you are just looping through the list to get the objects.

娇俏 2024-09-12 04:32:44

“高效”不是一个非此即彼的属性。这是相对的,就像大老鼠可能不比小象大一样。

这取决于你还在做什么。

您的情况可能会有所不同,但根据我的经验,虽然 ArrayList 可能比 List“慢”,但我从来没有在任何其他方面做的事情如此之少。非常明显。

也就是说,让编译器为我进行类型检查是件好事,而且不必强制转换也很好。

"Efficient" is not an either-or property. It is relative, just as a big mouse is probably not bigger than a small elephant.

It depends on what else you are doing.

Your mileage may vary, but in my experience, while ArrayList may be "slower" than List<T>, I have never been doing so little else that it was in any way noticeable.

That said, it is nice to have the compiler doing type-checking for me, and it is nice not having to cast things.

昇り龍 2024-09-12 04:32:44

如果对象来自非托管代码,并且您不需要添加或删除对象,则 Grid_Heading_Blk 数组可能比 List 更有效。如果您可以不使用数组,那么使用 for 循环可能会比 foreach 稍快一些。

If the objects are coming from unmanaged code, and you don't need to add or remove objects, then an array of Grid_Heading_Blk might be more efficient than a List. If you can get away with using an array, using a for loop might be slightly faster than foreach.

悍妇囚夫 2024-09-12 04:32:44

You can filter all elements of arrayList that can be cast to the same type using Enumerable.OfType(Of TResult) Method

List<MyClass> typedList = arrayList.OfType<MyClass>().ToList();

(based on suggestion in http://www.codeproject.com/Tips/68291/Convert-ArrayList-to-a-Generic-List )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文