Use of collections ArrayList and Hashtable in C #

Source: Internet
Author: User

use of collections ArrayList and Hashtable in C #

http://blog.csdn.net/linukey/article/details/42506819

ArrayList:

One.

Precautions:

1. You can specify the initial size of the ArrayList collection

var list = new ArrayList (10);//accommodates 10 elements

If you do not specify a size, the default size is 0, add one after 4, and then increment in multiples.

2.ArrayList is a complex version of the array, ArrayList internally encapsulates an array of type Object, which, in general terms, has no essential difference, or even many methods of ArrayList, such as index, INDEXOF, Contains, sort, and so on are the corresponding methods that call array directly on the basis of an internal array.

3. Impact of the internal object type: For the general reference type, this part of the impact is not very large, but for value types, adding and modifying elements inside the ArrayList will cause boxing and unboxing operations, and frequent operations may affect some of the efficiency. You should replace ArrayList with a generic class.

4. Array expansion: This is a factor that has a greater impact on ArrayList efficiency. Whenever a method of adding elements, such as Add, AddRange, Insert, Insertrange, is checked, the capacity of the internal array is not enough, if not enough, it will reconstruct an array at twice times the current capacity, copy the old elements into the new array, and discard the old array. At this point in the expansion operation, it should be compared to the impact of efficiency.

5. Frequent calls to IndexOf, contains and other methods (Sort, BinarySearch and other methods optimized, not excluded) caused by the loss of efficiency, first of all, we have to make it clear that ArrayList is a dynamic array, It does not include fast access through key or value algorithm, so actually call IndexOf, contains and other methods is to execute a simple loop to find the element, so frequent calls to this kind of method is not more than your own write loop and a little optimization to the fast, if there is this requirement, It is recommended to use a collection of key-value pairs such as Hashtable or SortedList.

Two:

Properties and Methods

Property:

1.Count: Currently ArrayList contains the number of elements, this property is read-only.

2.Capacity: The maximum number of ArrayList currently available can be set manually, but an exception is thrown when set to less than the count value.

Method:

1.Int Add (object value);

Used to add an element to the end of the current list.

2.Int AddRange (object value);

Used to add a collection or array to the end of the current list.

3.void Remove (object obj);

Used to delete an element, which is removed by a reference to the element itself.

4.void RemoveAt (int index);

Used to delete an element that is deleted by the index value.

5.void removerange (int index, int count);

Used to delete a collection or array, by specifying the starting index and the number of deletions to delete.

6.void Insert (int index, object value)

Used to add an element to a set position, and the elements behind the list move backwards.

7.void insetrange (int index, Icollection collec)

Used to add a collection or array from a specified location

8.void Sort ()

To sort the elements in a collection

9.void Reverse ();

Reverses the order of ArrayList or one of its vufenzhong elements

Int IndexOf (object);

int IndexOf (object, int);

int IndexOf (object, int, int);

Returns the zero-based index of the first occurrence of a value in ArrayList or part of it. No return-1 found.

11.Int LastIndexOf (object)

int LastIndexOf (object, int)

int LastIndexOf (object, int, int)

Returns the zero-based index of a ArrayList or part of it that is worth the last occurrence. Not found back-1

12.void trimsize ()

Used to pin the ArrayList to the actual element size, this method can be called to free the free memory when the dynamic array element is determined to no longer be added

13.void Clear ()

Empty all elements in the ArrayList

14.Array ToArray ()

This method copy the elements in the ArrayList into a new array

Hashtable

Common methods:

1. Add a KeyValue key value pair to the hash table: Hashtableobject.add (Key,value);

2. Remove a KeyValue key value pair in the hash table: Hashtableobject.remove (key);

3. Remove all elements from the hash table: Hashtableobject.clear ();

4. Determine if the hash table contains a specific key key:HashtableObject.Contains (key);

Use of collections ArrayList and Hashtable in C #

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.