且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

数组与ArrayList

更新时间:2023-02-26 22:18:52

数组是固定大小的。一个ArrayList负责你自己管理的大小调整,但是这样做是因为不是类型安全的。


你当然可以分配比你知道的更大的数组你需要在分配点和tehn用你发现的新东西填充额外的元素。或者甚至更好,如果你知道hte人口的fina大小预先分配这个大小的数组。但至于原来的问题呢?是的,如果您愿意自己管理重新分配,则不需要切换到ArrayList。


很明显,为什么你这么担心切换到一个ArrayList?


问候


Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


是不是我无法动态添加一个项目到一个数组?例如


public char [] = {" a"," b"};

char.Add(" newitem");

我真的必须切换到ArrayList才能这样做吗?

Arrays are fixed size. An ArrayList takes care of the resizing you would have to manage yourself but does so in a generaic way so isn''t typesafe.

You can of course allocate an array larger than you know you need at the point of allocation and tehn fill the extra elements with the new stuff you discover. Or even better if you know the fina size of hte population allocate an array of this size upfront. But as to the original question? Yes its true, no you don''t need to switch to an ArrayList if you''re happy to manage the reallocation yourself.

Incidently, why are you so worried about switching to an ArrayList?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

is it true that I cannot dynamically add an item to an array? Eg

public char[] = {"a","b"};
char.Add("newitem");
Do I really have to switch to ArrayList to do this?


thx Richard

我好吗?我正在做一个刽子手应用程序并将猜对的字母添加到

字符串数组中。在一个实例中,我必须使用

IndexOf()方法搜索该特定数组,但它在ArrayList上更复杂,因为

项是对象类型...


" Richard Blewett [DevelopMentor]" < RI ****** @ NOSPAMdevelop.com>写在

消息新闻:OL ************** @ TK2MSFTNGP10.phx.gbl ...
thx Richard
well I''m doing a hangman application and add upp the guessed letters to a
string Array. In one instance I have to search that particular array using
IndexOf() method but it is more complicated on ArrayList since there the
items are of object type...

"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:OL**************@TK2MSFTNGP10.phx.gbl...
数组是固定大小的。 ArrayList负责调整你自己管理的大小,但是这样做是因为不是类型安全的。

你当然可以分配比你知道的更大的数组。你需要在分配点和tehn用你发现的新东西填充额外的元素。或者更好的是,如果你知道hte人口的fina大小
预先分配这个大小的数组。但至于最初的问题呢?
是的,如果您对自己管理重新分配感到满意,不需要切换到ArrayList。
>你为什么这么担心转换到ArrayList?



Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

是不是我无法动态添加项目到数组?例如

public char [] = {" a"," b"};
char.Add(" newitem");

我是真的要切换到ArrayList来做这个吗?
Arrays are fixed size. An ArrayList takes care of the resizing you would
have to manage yourself but does so in a generaic way so isn''t typesafe.

You can of course allocate an array larger than you know you need at the
point of allocation and tehn fill the extra elements with the new stuff
you discover. Or even better if you know the fina size of hte population
allocate an array of this size upfront. But as to the original question?
Yes its true, no you don''t need to switch to an ArrayList if you''re happy
to manage the reallocation yourself.

Incidently, why are you so worried about switching to an ArrayList?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

is it true that I cannot dynamically add an item to an array? Eg

public char[] = {"a","b"};
char.Add("newitem");
Do I really have to switch to ArrayList to do this?



我想你知道那个数组的最大大小(你的字母表中的字母数)所以只需分配一个大小的数组,然后跟踪你插入的最后一个索引


问候


Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk >

thx Richard

我正在做一个刽子手应用程序并将猜测的字母添加到

字符串数组中。在一个实例中,我必须使用

IndexOf()方法搜索该特定数组,但它在ArrayList上更复杂,因为

项是对象类型...

Well I guess you know the maximum size of the array then (the number of letters in your alphabet) so just allocate an array of that size and just keep a track of the last index you inserted at

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

thx Richard
well I''m doing a hangman application and add upp the guessed letters to a
string Array. In one instance I have to search that particular array using
IndexOf() method but it is more complicated on ArrayList since there the
items are of object type...