使用数组或List更好吗?(复制)

时间:2022-11-19 12:10:53

This question already has an answer here:

这个问题已经有了答案:

I was wondering which type would have better performance and which you think should be used.

我想知道哪种类型会有更好的性能,你认为应该使用哪种类型。

For example I have a List of strings not knowing how many items I will need so having the .Add(String) function is really convenient. I can Add new strings to the list at any time easily.

例如,我有一个字符串列表,不知道需要多少项,所以使用. add (String)函数非常方便。我可以随时向列表中添加新的字符串。

What are the advantages/disadvantages of using each?

使用它们的优点/缺点是什么?

Are lists the new arrays?

是否列出了新数组?

8 个解决方案

#1


36  

More context is really required to answer the question properly:

要正确地回答这个问题,确实需要更多的上下文:

In a public API, you should try to use abstract collection types, so that you can change the internal implementation later if you need to.

在公共API中,您应该尝试使用抽象集合类型,以便在需要时可以稍后更改内部实现。

  • If the collection should not be changed by the outside world, use IEnumerable<T>.
  • 如果集合不应由外部世界更改,请使用IEnumerable
  • If the collection will be changed by the outside world, use ICollection<T>.
  • 如果收藏会被外界改变,使用ICollection
  • If indexed access is required, use IList<T>.
  • 如果需要索引访问,请使用IList

In a private implementation, it's not as important to use the abstract types:

在私有实现中,使用抽象类型并不重要:

  • If you need indexed access and know the final size, use T[] or List<T>.
  • 如果需要索引访问并知道最终大小,请使用T[]或列表
  • If you need indexed access and don't know the final size, use List<T>.
  • 如果您需要索引访问,并且不知道最终的大小,请使用List
  • If you plan to access elements in a LIFO pattern, use Stack<T>.
  • 如果您计划访问LIFO模式中的元素,请使用Stack
  • If you plan to access elements in a FIFO pattern, use Queue<T>.
  • 如果您计划访问FIFO模式中的元素,请使用Queue
  • If you need to access elements at the beginning and end of the list, but not in the middle, use LinkedList<T>.
  • 如果您需要访问列表开头和结尾的元素,而不是中间的元素,请使用LinkedList
  • If you don't want duplicates, use HashSet<T>.
  • 如果不想要重复,可以使用HashSet

In .NET 4.0 you have a few more choices, but those are the basics.

在。net 4.0中,你有更多的选择,但这些都是基础。

#2


22  

List<String> is implemented using an array String[].

List 使用数组字符串[]实现。

If you don't know how many elements you'll have, use List<String>

如果不知道有多少元素,可以使用List

You can give the estimated (or maximum) number of elements you expect in the capacity constructor parameter (new List<String>(10)), this will be the initial size of the underlying array.

您可以在capacity构造函数参数(new List (10))中给出预计的(或最大)元素数量,这将是底层数组的初始大小。

When you Add() an item and there is no room for this item, the underlying array is copied to a new array of double the size.

当您添加()一个项目,并且没有这个项目的空间时,基础数组将被复制到一个新数组中,该数组的大小是原来的两倍。

What I do: when I know the exact size of the collection and I know I won't change the size of the collection, I use an array (String[]). Otherwise I use a List<String>.

我所做的:当我知道集合的确切大小并且我知道我不会改变集合的大小时,我使用一个数组(String[])。否则我使用列表

By the way, this goes for any type and not just String.

顺便说一下,这适用于任何类型而不仅仅是字符串。

#3


7  

It depends on usage scenario, BUT it's also a micro-optimisation until you have identified a bottleneck by profiling. Use whatever fits the usage best.

它依赖于使用场景,但它也是一种微优化,直到您通过分析确定了瓶颈。使用最适合使用的。

#4


3  

Use List<> in most all cases, and don't worry about performance. There is a good chance you will go through your entire career and never need to performance tune by converting a List<> into an array.

在大多数情况下使用List<>,不要担心性能。您很有可能会经历整个职业生涯,并且不需要通过将列表<>转换为数组来调整性能。

#5


3  

In most scenarios the performance difference is not appreciable so I would use List<string> since it provides much more functionality that could be useful in different situations.

在大多数情况下,性能差异并不明显,因此我将使用List ,因为它提供了很多在不同情况下可能有用的功能。

#6


2  

If you don't know the size of items to be added, always go for List<string> than string array.

如果您不知道要添加的项的大小,请始终使用List > string array。

#7


2  

If you need dynamic sizing, then go with List<string>.

如果需要动态调整大小,则使用List

If you're worried about performance, then I would suggest starting with List<string> and see if there's really an issue. It uses arrays internally so I would think, for the most part, there should be no performance issues.

如果您担心性能,那么我建议从List 开始,看看是否真的有问题。它在内部使用数组,所以我认为,在大多数情况下,不应该存在性能问题。

If you have a staticly sized collection, you could still use string[].

如果您有一个静态大小的集合,您仍然可以使用string[]。

#8


2  

Of course it depends on your application, but in circumstances List<string> (or even just IEnumerable<string> is preferable.

当然,这取决于您的应用程序,但是在环境列表中 (或者甚至只是IEnumerable 更可取)。

#1


36  

More context is really required to answer the question properly:

要正确地回答这个问题,确实需要更多的上下文:

In a public API, you should try to use abstract collection types, so that you can change the internal implementation later if you need to.

在公共API中,您应该尝试使用抽象集合类型,以便在需要时可以稍后更改内部实现。

  • If the collection should not be changed by the outside world, use IEnumerable<T>.
  • 如果集合不应由外部世界更改,请使用IEnumerable
  • If the collection will be changed by the outside world, use ICollection<T>.
  • 如果收藏会被外界改变,使用ICollection
  • If indexed access is required, use IList<T>.
  • 如果需要索引访问,请使用IList

In a private implementation, it's not as important to use the abstract types:

在私有实现中,使用抽象类型并不重要:

  • If you need indexed access and know the final size, use T[] or List<T>.
  • 如果需要索引访问并知道最终大小,请使用T[]或列表
  • If you need indexed access and don't know the final size, use List<T>.
  • 如果您需要索引访问,并且不知道最终的大小,请使用List
  • If you plan to access elements in a LIFO pattern, use Stack<T>.
  • 如果您计划访问LIFO模式中的元素,请使用Stack
  • If you plan to access elements in a FIFO pattern, use Queue<T>.
  • 如果您计划访问FIFO模式中的元素,请使用Queue
  • If you need to access elements at the beginning and end of the list, but not in the middle, use LinkedList<T>.
  • 如果您需要访问列表开头和结尾的元素,而不是中间的元素,请使用LinkedList
  • If you don't want duplicates, use HashSet<T>.
  • 如果不想要重复,可以使用HashSet

In .NET 4.0 you have a few more choices, but those are the basics.

在。net 4.0中,你有更多的选择,但这些都是基础。

#2


22  

List<String> is implemented using an array String[].

List 使用数组字符串[]实现。

If you don't know how many elements you'll have, use List<String>

如果不知道有多少元素,可以使用List

You can give the estimated (or maximum) number of elements you expect in the capacity constructor parameter (new List<String>(10)), this will be the initial size of the underlying array.

您可以在capacity构造函数参数(new List (10))中给出预计的(或最大)元素数量,这将是底层数组的初始大小。

When you Add() an item and there is no room for this item, the underlying array is copied to a new array of double the size.

当您添加()一个项目,并且没有这个项目的空间时,基础数组将被复制到一个新数组中,该数组的大小是原来的两倍。

What I do: when I know the exact size of the collection and I know I won't change the size of the collection, I use an array (String[]). Otherwise I use a List<String>.

我所做的:当我知道集合的确切大小并且我知道我不会改变集合的大小时,我使用一个数组(String[])。否则我使用列表

By the way, this goes for any type and not just String.

顺便说一下,这适用于任何类型而不仅仅是字符串。

#3


7  

It depends on usage scenario, BUT it's also a micro-optimisation until you have identified a bottleneck by profiling. Use whatever fits the usage best.

它依赖于使用场景,但它也是一种微优化,直到您通过分析确定了瓶颈。使用最适合使用的。

#4


3  

Use List<> in most all cases, and don't worry about performance. There is a good chance you will go through your entire career and never need to performance tune by converting a List<> into an array.

在大多数情况下使用List<>,不要担心性能。您很有可能会经历整个职业生涯,并且不需要通过将列表<>转换为数组来调整性能。

#5


3  

In most scenarios the performance difference is not appreciable so I would use List<string> since it provides much more functionality that could be useful in different situations.

在大多数情况下,性能差异并不明显,因此我将使用List ,因为它提供了很多在不同情况下可能有用的功能。

#6


2  

If you don't know the size of items to be added, always go for List<string> than string array.

如果您不知道要添加的项的大小,请始终使用List > string array。

#7


2  

If you need dynamic sizing, then go with List<string>.

如果需要动态调整大小,则使用List

If you're worried about performance, then I would suggest starting with List<string> and see if there's really an issue. It uses arrays internally so I would think, for the most part, there should be no performance issues.

如果您担心性能,那么我建议从List 开始,看看是否真的有问题。它在内部使用数组,所以我认为,在大多数情况下,不应该存在性能问题。

If you have a staticly sized collection, you could still use string[].

如果您有一个静态大小的集合,您仍然可以使用string[]。

#8


2  

Of course it depends on your application, but in circumstances List<string> (or even just IEnumerable<string> is preferable.

当然,这取决于您的应用程序,但是在环境列表中 (或者甚至只是IEnumerable 更可取)。