int[] x = new int[5];
x[0] = 1;
x[1] = 2;
x[2] = 3;
x[3] = 4;
x[4] = 5;
System.Collections.ArrayList y = new System.Collections.ArrayList(5);
y.Add(1);
y[2] = 2;
The above gives a run time exception "Index was out of range. Must be non-negative and less than the size of the collec开发者_如何学编程tion."
Why is this so ? Can't we add data into ArrayList using index same as int[] Array? Please provide me with some pointers to understand the reason behind this implementation.
Your ArrayList
only has one entry in it, and so index 2
is out of range. ArrayList#Add
adds to the list. Your list has an initial capacity of 5
because you called the ArrayList(Int32)
constructor, but you've only added one actual entry to it (which will be at index 0
).
The only thing you seem to have misunderstood is what the ArrayList
constructor does. new ArrayList(5)
does not create an ArrayList
with five elements, as opposed to new int[5]
, which does create an array with five elements (all having the value zero). A newly-created ArrayList
is always empty, so any attempt to use []
to set the value of any element will crash, because there are no elements. This is the same behavior as a regular array if you had created an array by saying new int[0]
- any attempt to index into it would crash. The only way to get a five-element ArrayList
is to either use the constructor that takes a (five-element) collection as a parameter (as @Stilgar and @Kelon showed), or by calling e.g. Add(0)
five times. After having done this, you can access x[0]
, x[1]
, ..., x[4]
.
What does new ArrayList(n)
do, then? It creates an ArrayList
of size zero, but where the internal array that is used to store the values is given the size n, so that we can add n elements before the internal array must be replaced by a bigger one (which takes a little bit of time, which is why you in high-performance scenarios might want to use this constructor if you know how big the list will eventually become).
new ArrayList(5)
just allocates memory for 5 entries. That's all. You still have to add or insert to a list - it's not the same as an array.
Maybe you need to use a collection rather than an array like a List<int>();
As for why see this
http://msdn.microsoft.com/en-us/library/k2604h5s(v=vs.80).aspx
Class library designers might need to make difficult decisions about when to use an array and when to return a collection. Although these types have similar usage models, they have different performance characteristics. In general, you should use a collection when Add, Remove, or other methods for manipulating the collection are supported.
For more information about using collections, see Collections and Data Structures.
Arrays initializes the memory with default values. Lists have different semantics. They are supposed to manage their size dynamically. The capacity you've provided (5) is purely optimization based on the specific list implementation. Not every IList implementation would have the same internal representation. What if the implementation was a LinkedList? You need to think about the concept of the data structure List not about the particular implementation.
If you want to assign values there are plenty of ways to initialize a list. You can pass an array to the constructor or use the collection initializers from C# 3.0. And please use List instead of ArrayList.
//Collection initializer
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
//passing array as an argument to the constructor
int[] ints = new[] { 1, 2, 3, 4, 5 };
List<int> list2 = new List<int>(ints);
Try
new ArrayList(new int[5]);
this initializes the ArrayList with 5 given default entries.
But
y.Add(1); // would add a 6t entry
You can specify the capacity when creating the ArrayList
, but this is only so that the object can allocate space internally, the initial size of the list is still zero.
The difference between an ArrayList
and an array is that the size of the list is dynamic. You can add and remove items, which is not possible with an array. It makes sense for a dynamically sized collection to start with the size zero, instead of filling it with zero-values.
Note: The ArrayList
class is practically obsolete; you should use the generic List<T>
class instead, which performs better and is easier to use, especially for value types like int
.
精彩评论