How to Use ArrayList

Source: Internet
Author: User

1What isArrayList
ArrayList is the legendary dynamic Array. In MSDN, It is the complex version of Array. It provides the following benefits:

  • Dynamically add and remove elements
  • ICollection and IList interfaces are implemented.
  • Flexible array size setting

2 , How to use ArrayList
The simplest example:
ArrayList List = new ArrayList ();
For (int I = 0; I <10; I ++) // Add 10 Int elements to the array.
List. Add (I );
//... The program does some processing
List. RemoveAt (5); // remove the 6th Elements
For (int I = 0; I <3; I ++) // Add three more elements.
List. Add (I + 20 );
Int32 [] values = (Int32 []) List. ToArray (typeof (Int32); // returns the array contained in ArrayList

This is a simple example. Although it does not contain all the methods of ArrayList, it can reflect the most common usage of ArrayList.

3 , ArrayList Important methods and attributes
(1) constructor
ArrayList provides three constructors:
Public ArrayList ();
The default constructor initializes the internal array with the default size (16 ).
Public ArrayList (ICollection );
Construct with an ICollection object and add the elements of the set to ArrayList
Public ArrayList (int );
Initializes an internal array with the specified size.

(2) IsSynchronized attributes 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. Every attribute in it uses the lock keyword to ensure thread synchronization. * *** However, using this method (ArrayList. Synchronized) does not guarantee the synchronization of enumeration. For example, one thread is deleting or adding collection items, and the other thread is enumerating at the same time.

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.