ArrayList Overview of C #

Source: Internet
Author: User

ArrayList class is equivalent to an advanced dynamic array . is a Array The upgrade version of the class.

Overview:

ArrayList is located System.Collections under Namespaces

you can delete and add elements on the fly , in a way, in a sense, , He's an advanced version of the array. , but not the same as an array.

compared to arrays , ArrayList class provides the following features:

? ? 1. The length of the array is fixed ( once declared , cannot be changed beyond the bounds of the array ); The length of the ArrayList can be automatically expanded as needed.

? ? 2. ArrayList provides methods for adding, deleting, and inserting elements within a range ; You can only get or set the value of an element one time in an array.

? ? 3. ArrayList provides methods for wrapping and returning a read-only and fixed-size package to a collection ; The array is not provided.

? ? 4. ArrayList can only be one-dimensional form ; the array can be multidimensional.

ArrayList How to declare :

? ? ArrayList provides 3 constructors ,? The corresponding is the 3 method of declaration :

? ? [1]. the default constructor initializes the internal array with the default size (

? ? ? ? public ArrayList ();

? ? ? ? statement:

? ? ? ? ArrayList list = new Arrarlist ();

? ? ? ? Example : ( declares a ArrayList object and Adds ten int Elements of type )

? ? ? ? ArrayList ArrayList = new ArrayList ();

? ? ? ? for (int i = 0; i <; i++)

? ? ? ? {

? ? ? ? ? ? Arraylist.add (i);

? ? ? ? }

? ? [2]. construct with a ICollection object and add the elements of the collection to the ArrayList :

? ? ? ? Public ArrayList (ICollection);

? ? ? ? statement:

? ? ? ? ArrayList ArrayList = new ArrayList (arrayname);? //arrayname: to add to The name of the array in the ArrayList

? ? ? ? Example: ( add a one-dimensional int array to the ArrayList collection )

? ? ? ? int[] array = new int[] {1,2,3,4,5,6,7};

? ? ? ? ArrayList ArrayList = new ArrayList (array);

? ? [3]. Initialize the inner array with the specified size

? ? ? ? public ArrayList (int);

? ? ? ? statement:

? ? ? ? ArrayList ArrayList = new ArrayList (10);

? ? ? ? Example: ( declare a ArrayList collection with a length of ten and add elements dynamically )

? ? ? ? ArrayList ArrayList = new ArrayList (10);

? ? ? ? for (int i = 0; i < Arraylist.count; i++)

? ? ? ? {

? ? ? ? ? ? ?//arraylist.count : Gets the length of the ArrayList object

? ? ? ? ? ? Arraylist.add (i);

? ? ? ? }

ArrayList Common Properties for:

bool, indicating whether access to ArrayList is synchronized

TD style= "PADDING-TOP:3PX; padding-left:4px; padding-bottom:3px; padding-right:4px; Border-top:none; Border-left:none; Border-bottom:solid #a3a3a3 1.0pt; Border-right:solid #a3a3a3 1.0pt ">

capacity

Get or set the length ArrayList can reach

count

Get the actual length of the ArrayList object

isfixedsize

bool, indicating whether ArrayList has a fixed size

isreadonly

bool, indicating whether ArrayList is read-only

issynchronized

item

Get or set the element at the specified index

SyncRoot

Gets the object that can be used to synchronize ArrayList access

ArrayList element Additions:

? ? the Add () and Insert () methods are provided in ArrayList for adding elements to the ArrayList Collection :

? ? [1]. ADD ():// or AddRange ()

? ? ? ? Add () cannot specify the location of elements added to the collection , typically at the end of the ArrayList collection.

? ? ? ? Syntax:

? ? ? ? Arraylist.add (Object value);

? ? ? ? Arraylist.addrange (Array)

? ? ? ? ?//value: to add to The Object at the end of the ArrayList , which can be a null reference and also allows duplicate values ;

? ? [2]. Insert ():// or insert

? ? ? ? The Insert () method is used to add elements to the collection at the specified position ( in index )

? ? ? ? Syntax:

? ? ? ? Arraylist.insert (int index, Object value);

? ? ? ?? //index: from 0 -Starting index

???? ?//value: with Add ()

? ? ? ? Description:

? ? ? ? If you say ArrayList the number of elements actually stored is equal to ArrayList number of elements that can be stored , is incremented by automatically reallocating the internal array ArrayList of Capacity ( i.e. length ), and copy the existing elements between the new elements into the new array.

? ? ? ? ? ???

ArrayList with the Array conversion of each other:

ArrayList Overview of C #

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.