Collections in C# .NET (The ArrayList)
An array list is a dynamic array. Although indigenous arrays are static, elements can be added or removed from an ArrayList at run time. Elements of the ArrayList are not automatically sorted. Similar to single-dimensional arrays, the elements of an ArrayList are accessible using the indexing operator and indices.
In addition to the standard collection interfaces, ArrayList implements the IList interface. The following Table lists the ArrayList-specific methods and properties. The static members of ArrayList are thread-safe, whereas instance members are not. The common collection interfaces—ICollection, IEnumerable, and ICloneable—are not discussed in detail.The following code uses various ArrayList methods and properties. It creates a new ArrayList, which is then initialized with command-line parameters. The Add method is called to add elements to the ArrayList. The ArrayList is then sorted and cloned. Then the values at the elements of the cloned ArrayList are doubled. Afterward, the cloned ArrayList is enumerated and every element is displayed.
|
Member Name |
Syntax |
|
Constructors |
ArrayList() ArrayList( ICollection sourceCollection) ArrayList(int capacity) |
|
Adapter This method creates a wrapper for an IList collection. |
static ArrayList Adapter( IList list) |
|
Add This method adds an element to the end of the ArrayList collection. |
virtual int Add(object value) |
|
AddRange This method adds a range of elements to the ArrayList collection. The elements are input from an ICollection type, such as a regular array. |
virtual void AddRange( ICollection elements) |
|
BinarySearch This method performs a binary search for a specific value in a sorted array. |
virtual int BinarySearch( object value) virtual int BinarySearch( object value, IComparer comparer) virtual int BinarySearch( int index, int count, object value, IComparer comparer) |
|
Capacity This property gets or sets the number of properties allowed in the collection. The default capacity is 16 elements. Capacity is different from the number of elements. The capacity is automatically increased as elements are added. When the number of elements exceeds the current capacity, the capacity doubles. Capacity is for better memory management of elements in the collection. |
virtual int Capacity { get; set} |
|
Clear This method removes all the elements of the collection. |
virtual void Clear() |
|
Contains This method returns true if the specified item is found in a collection. If the item is not found, false is returned. |
virtual bool Contains(object item) |
|
Count This property returns the number of elements in the collection. |
virtual int Count{ get; } |
|
FixedSize This method creates a wrapper for an ArrayList or IList collection, in which elements cannot be added or removed. |
static ArrayList FixedSize( ArrayList sourceArray) static IList FixedSize( IList sourceList) |
|
GetRange This method returns a span of elements from the current array. The result is stored in a destination ArrayList. |
virtual ArrayList GetRange( int index, int count) |
|
IndexOf This method returns the index of the first matching element in the collection. |
virtual int IndexOf( object value) virtual int IndexOf(object value, int startIndex) virtual int IndexOf(object value, int startIndex, int count) |
|
Insert This method inserts an element into the collection at the specified index. |
virtual void Insert(int index, object value) |
|
InsertRange This method inserts multiple elements into the collection at the specified index. |
virtual void InsertRange( int index, ICollection sourceCollection) |
|
IsFixedSize This property returns true if the collection is fixed-length. Otherwise, the property returns false. |
virtual bool IsFixedSize{ get;} |
|
IsReadOnly This property returns true if the collection is read-only. Otherwise, the property returns false. |
virtual bool IsReadOnly{ get;} |
|
Item This property gets or sets the element at the index. |
virtual object this[int index] { get;set;} |
|
LastIndexOf This method returns the index of the last matching element in the collection. |
virtual int LastIndex( object value) virtual int LastIndexOf(object value, int startIndex) virtual int LastIndexOf(object value, int startIndex, int count) |
|
ReadOnly This method creates a read-only wrapper for an IList object. |
static ArrayList ReadOnly( ArrayList sourceArray) static IList ReadOnly( IList sourceList) |
|
Remove This method removes the first element in the collection that matches the value. |
virtual void Remove( object value) |
|
RemoveAt This method removes the element at the index from the collection. |
virtual void RemoveAt( int index) |
|
RemoveRange This method removes a range of elements from a collection. |
virtual void RemoveRange( int index, int count) |
|
Repeat This method returns an ArrayList with each element initialized to the same value. Count is the number of times to replicate the value. |
static ArrayList Repeat( object value, int count) |
|
Reverse This method reverses the order of elements in the collection. |
virtual void Reverse() virtual void Reverse( int beginIndex, int endingIndex) |
|
SetRange This method copies elements from a collection into the same elements in the ArrayList collection. |
virtual void SetRange( int index, ICollection sourceCollection) |
|
Sort This method sorts an ArrayList. |
virtual void Sort() virtual void Sort( IComparer comparer} virtual void Sort(int index, int count, IComparer comparer) |
|
Synchronized This method returns a thread-safe wrapper of an ArrayList or IList object. |
static ArrayList Synchronized( ArrayList sourceArray) static IList Synchronized( IList sourceList) |
|
ToArray This method copies elements from the current array into a new collection. |
virtual object [] ToArray() virtual Array ToArray(Type type) |
|
TrimToSize This method sets the capacity to the number of elements in the collection. |
virtual void TrimToSize() |
|
IEnumerable members |
GetEnumerator |
|
ICloneable members |
Clone |
|
ICollection members |
CopyTo, Count, IsSynchronized, and SyncRoot |
using System; using System.Collections; namespace Examples.Collection {
public class Starter
{
public static void Main(string [] argv)
{
ArrayList al1=new ArrayList();
foreach(string arg in argv)
{
al1.Add(int.Parse(arg));
}
al1.Sort();
ArrayList al2=(ArrayList)al1.Clone();
for(int count=0;count<al2.Count;++count)
{
al2[count]=((int)al2[count])*2;
}
foreach(int number in al2)
{
Console.WriteLine(number);
}
}
} }
3 Comments »
Leave a comment
-
Archives
- February 2009 (1)
- November 2008 (6)
- October 2008 (4)
- September 2008 (13)
- August 2008 (11)
- July 2008 (29)
- June 2008 (19)
- May 2008 (8)
-
Categories
-
RSS
Entries RSS
Comments RSS


Default Capacity of arraylist is not 16 Elements. it is 0(Zero).
Comment by Sachin | June 5, 2009 |
Dear Sachin,
Sorry for the late reply.
See the following links if you have any confusion.
http://en.csharp-online.net/Hello,_CSharp%E2%80%941.13_Dynamic_Arrays:_The_ArrayList_Collection_Class
http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=130
/Author
Comment by shiman | June 11, 2009 |
hats of to the knowlege better luck in the future
all the best
Comment by Shreyas Gowda T | October 11, 2009 |