Archived

This topic is now archived and is closed to further replies.

thedevdan

ArrayList problems...

Recommended Posts

The Capacity property is supposed to set the total size of the list. However, if I set the size to 1, then access the first element with [0], an ArguementOutOfRange exception is thrown. However, if I simply Add something to the list, then access the first element (without setting Capacity), everything is fine. Why is this? And three more quick questions, while I am here: 1. Does the "using" statement (as in using System) only have an effect on some statements? If I say "using System;" at the top of my program, I can type "Console." whatever, but I can't say "Collections." whatever. The compiler says that it can't find it (putting System.Collections works fine). 2. On MSDN, where can I find info on class' operators? It only shows methods, fields, and properties. 3. Why does the indexOf method of ArrayList return "-1" if the object isn't there, and not throw an exception!? Sorry it's a lot of questions, but they are all bugging me. [edited by - thedevdan on May 19, 2004 7:54:27 PM] [edited by - thedevdan on May 19, 2004 7:54:45 PM]

Share this post


Link to post
Share on other sites
ArrayList.Capacity refers to how many elements the ArrayList can hold, not how many it does hold. If you set the capacity of an empty ArrayList without adding any elements, it''s still an empty ArrayList.

1. Console is a class in namespace System. Collections is a different namespace.

2. Shows the operators to me.

3. Because the element being searched for not being in an ArrayList is not an exceptional situation.

Share this post


Link to post
Share on other sites
quote:
Original post by SiCrane
ArrayList.Capacity refers to how many elements the ArrayList can hold, not how many it does hold. If you set the capacity of an empty ArrayList without adding any elements, it''s still an empty ArrayList.

1. Console is a class in namespace System. Collections is a different namespace.

2. Shows the operators to me.

3. Because the element being searched for not being in an ArrayList is not an exceptional situation.


Great answers.

I guess I missunderstood how "using" works. It doesn''t expose nested namespaces?

About the operators... well, I should have said "indexers". But now I understand why it is throwing an exception.

Share this post


Link to post
Share on other sites
using only exposes types. A namespace is not a type. Annoying, isn''t it?

Indexers are exposed through the Item property of a class.

Share this post


Link to post
Share on other sites