C # ArrayList of a set

Source: Internet
Author: User

1. ArrayList Definition

The System. Collections. ArrayList class is a special array (that is, a dynamic array ).

By adding and deleting elements, You can dynamically change the length of the array.

2. Advantages

Dynamically add and delete Elements

ICollection and IList interfaces are implemented.

Flexible array size setting

3. ArrayList Constructor

Constructors

Note

Public ArrayList ();

The default constructor initializes the internal array with the default size (16 ).

Public ArrayList (ICollection );

Construct with an object that implements the ICollection interface, and add the elements of this set to ArrayList

Public ArrayList (int );

Initializes an internal array with the specified size.

4. ArrayList attributes

Attribute name

Note

Count

Currently, ArrayList contains the number of elements. This attribute is read-only.

Capacity

Currently, ArrayList can contain the maximum number. You can manually set this attribute. However, when it is set to a value smaller than the Count value, an exception is thrown.

Note: Capacity is the number of elements that can be stored in ArrayList. Count is the number of actually contained elements in the ArrayList. Capacity is always greater than or equal to Count. If the value of Count exceeds the Capacity when an element is added, the Capacity of the list is automatically increased.
If the Capacity value is explicitly set, the internal array also needs to be reassigned to accommodate the specified Capacity. If Capacity is explicitly set to 0, the common language runtime sets it to the default Capacity. The default capacity is 16.
After Clear is called, Count is 0, and Capacity is the default Capacity of 16 instead of 0.

5. ArrayList Method

Method Name

Note

Int Add (object value );

Adds an element to the end of the current list.

 

Used to add a batch of elements to the end of the current list

Void Remove (object obj );

Deletes an element by referencing it.

Void RemoveAt (int index );

Used to delete an element and use the index value to delete it.

Void RemoveRange (int index, int count );

Deletes a batch of elements by specifying the start index and the number of deleted elements.

Void Insert (int index, object value)

Used to add an element to a specified position, and the elements following the list move in turn

Void InsertRange (int index, Icollection collec)

Used to add a batch of elements starting from the specified position.

Void Sort ()

Sorts the elements in an ArrayList or a part of it.

Void Reverse ();

Reverses the order of elements in an ArrayList or a part of it.

Int IndexOf (object)

Int IndexOf (object, int)

Int IndexOf (object, int, int)

Returns the index starting from scratch for the first matching item of a value in the ArrayList or part of it. -1 is not found.

Int LastIndexOf (object)

Int LastIndexOf (object, int)

Int LastIndexOf (object, int, int)

Returns the index starting from scratch for the last matching item of a value in the ArrayList or part of it. -1 is not found.

 

Bool Contains (object)

Determines whether an element is in the ArrayList. True is returned for inclusion; otherwise, false is returned.

Void TrimSize ()

This method is used to fix the ArrayList to the actual size of the element. When the dynamic array element is determined not to be added, you can call this method to release the free memory.

Void Clear ();

Clear all elements in the ArrayList

Array ToArray ()

This method copies the ArrayList elements to a new array.

 

6. Precautions for using ArrayList

1. IsSynchronized attribute and ArrayList. Synchronized Method
The IsSynchronized attribute indicates whether the current ArrayList instance supports thread synchronization, while the static method of ArrayList. Synchronized returns the encapsulation of a thread synchronization of ArrayList.
If a non-thread synchronization instance is used, you need to manually call lock to maintain thread synchronization during multi-thread access. For example:
ArrayList list = new ArrayList ();
Lock (list. SyncRoot) // when ArrayList is not packaged in a thread, the SyncRoot attribute is actually its own. However, to meet the SyncRoot definition of ICollection, SyncRoot is used to maintain the standardization of source code.
{
List. Add ("Add a Item ");
}

If you use ArrayList. the instance returned by the Synchronized method does not need to be considered for thread synchronization. This instance itself is thread-safe. In fact, ArrayList implements an internal class to ensure thread synchronization, ArrayList. synchronized returns an instance of this class, and every attribute in it uses

Lock keyword to ensure thread synchronization.

However, use this method (ArrayList. synchronized) does not guarantee the synchronization of enumeration. For example, if one thread is deleting or adding collection items, and the other thread is enumerating at the same time, enumeration will throw an exception. Therefore, you must use SyncRoot to lock the set during enumeration.

Hashtable and ArrayList Use thread security in a similar way.

 

2. ArrayList is a complex version of Array.
ArrayList encapsulates an array of the Object type. In general, it has no essential difference with the array, and even many methods of ArrayList, for example, Index, IndexOf, Contains, and Sort all directly call the corresponding method of Array Based on the internal Array.
3. Impact of internal Object Types
For general reference types, this part does not have a great impact, but for value types, adding and modifying elements to ArrayList will cause packing and unpacking operations, frequent operations may affect some efficiency.
4. array resizing
This is a factor that significantly affects ArrayList efficiency.
Each time you execute Add, AddRange, Insert, InsertRange, and other methods to Add elements, check whether the internal array capacity is insufficient. If yes, it will rebuild an array with twice the current capacity

Copy to the new array and discard the old array. The expansion operation at this critical point affects efficiency.
Example 1:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.