ArrayList problems...

Started by
3 comments, last by thedevdan 19 years, 12 months ago
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]
Not giving is not stealing.
Advertisement
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.
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.
Not giving is not stealing.
using only exposes types. A namespace is not a type. Annoying, isn''t it?

Indexers are exposed through the Item property of a class.
Thanks a lot for your replies. I ''preciate it.
Not giving is not stealing.

This topic is closed to new replies.

Advertisement