列表与LT; T>题 [英] List<T> Question

查看:85
本文介绍了列表与LT; T>题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我可以轻松使用字符串数组

时,我一直在使用List(of String)。


它仍然被认为是最佳做法使用字符串的通用列表而不是

然后是字符串数组?

谢谢

解决方案




Sean写道:

当我可以轻松地使用字符串数组时,我一直在使用List(of String)。

是否仍然认为最佳做法是使用Generic list of string而不是
然后是一个字符串数组?




使用任何适合场合的东西。 />

最重要的区别是string []是固定长度的,

List< string>是可变长度的。


请记住,它们都可以在创建之后使用它可以通过IList< string>来获得



-

Helge Jensen

mailto:他********** @ slog.dk

sip: he**********@slog.dk

- =>塞巴斯蒂安的封面音乐: http://ungdomshus.nu < = -


一个更重要的区别:

字符串数组为您提供插入成本O(1),

和类型字符串插入的数组列表成本是O(n)。

因此,字符串数组提供更好的性能,但使用起来不太舒服。

Sharon。


" Helge Jensen" <他********** @ slog.dk>在消息中写道

news:Om ************** @ TK2MSFTNGP10.phx.gbl ...


< Sean写道:

当我可以轻松使用字符串
数组时,我一直在使用List(of String)。

是否仍然认为最佳做法是使用Generic list of string而不是
然后是字符串数组?



使用适合的场合。

最多重要的区别是string []是固定长度的,而List< string>是可变长度的。

请记住,它们都可以在创建之后通过IList< string>来使用它。

-
Helge Jensen <邮件:他********** @ slog.dk
sip:他********** @ slog.dk
- =>塞巴斯蒂安的封面音乐: http://ungdomshus.nu < = -



>字符串插入成本的数组列表是O(n)


真的吗?为什么?来自哪里的信息?


据我所知,ArrayList插入成本为O(1),除非

ArrayList已满,在这种情况下必须在其他地方重新分配,并复制
以增加其容量。所以,这一切都取决于是否它首先被实例化(至少)足够大。


我认为ArrayList是支持的通过一个数组和一个使用的长度,并且

当数组变满时,数组被简单地复制到一个更大的数组。


对于传统的链接列表,插入成本通常也是
O(1),但搜索成本(对于linkedList [i])是O(n)。


I have been using List(of String) when I could easily be using a string array
instead.

Is it still considered best practice to use Generic list of string rather
then a string array?
Thanks

解决方案



Sean wrote:

I have been using List(of String) when I could easily be using a string array
instead.

Is it still considered best practice to use Generic list of string rather
then a string array?



Use whatever suits the occasion.

The most important difference is that string[] is fixed-length and
List<string> is variable-length.

Remember that they can both be used iterchangeable after creation
through IList<string>.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-


One more important difference:
The string array gives you insertion cost of O(1),
and array list of type string insertion cost is O(n).
So, string array gives better performance, but is less comfortable to use.
Sharon.

"Helge Jensen" <he**********@slog.dk> wrote in message
news:Om**************@TK2MSFTNGP10.phx.gbl...



Sean wrote:

I have been using List(of String) when I could easily be using a string
array
instead.

Is it still considered best practice to use Generic list of string rather
then a string array?



Use whatever suits the occasion.

The most important difference is that string[] is fixed-length and
List<string> is variable-length.

Remember that they can both be used iterchangeable after creation
through IList<string>.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-



> array list of type string insertion cost is O(n)

Really? Why? Where is that information from?

So far as I know, ArrayList insertion cost is O(1), unless the
ArrayList is full, in which case it must be reallocated elsewhere and
copied to increase its capacity. So, it all rather depends upon whether
it''s instantiated (at least) large enough in the first place.

I thought that ArrayList is backed by an array and a "used length", and
the array is simply copied to a bigger array when it gets full.

For a traditional linked list, the insertion cost is typically also
O(1), but the seek cost (for linkedList[i]) is O(n).


这篇关于列表与LT; T&GT;题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆