Use a set to organize relevant data and a set to organize relevant data

Source: Internet
Author: User
Tags unpack

Use a set to organize relevant data and a set to organize relevant data

ArrayList is very similar to an array, which is also called an array list. ArrayList can be dynamically maintained.

Tip:

Similar to arrays, the data stored in ArrayList is called an element. The number of elements that can be saved in ArrayList is the size of ArrayList. The default initial capacity is 0. You can access the elements in ArrayList through indexes, index starts from 0

ArrayList belongs to the System. Collections namespace and is a collection type.

Using System. Collection

ArrayList arr = new ArrayList ();

ArrayList common methods and attributes

Attribute Name Description

Count: obtains the number of actually contained elements in the ArrayList.

Return Value Type Method Name Description

Int Add (Object value) adds the Object to the end of ArrayList

Void RemoveAt (int index) removes the element at the specified index in ArrayList.

Void Remove (Object value) removes a specific Object from ArrayList

Void Clear () removes all elements from ArrayList

Add data to ArrayList

1. ArrayList Add elements using the Add Method

Returned value: the value is of the int type and is used to return the index of the element.

Parameter: If the elements added to the ArrayList are of the value type, these elements will be converted to the Object reference type and saved. Therefore, all the elements in the ArrayList are references to objects.

Access a single element in the ArrayList

The method used by ArrayList to obtain an element is the same as that of an array. It is also accessed through an index. The index of the first element in ArrayList is 0.

Traverse elements in ArrayList

Int [] array = new int [] {1, 2, 3, 4, 5 };

For (int I = 0; I <array. length; I ++ ){

Console. WriteLine (array [I]);
}

Delete an element from ArrayList

ArrayList engineers = new ArrayList ();

Engineers. RemoveAt (0 );

Enginneers. Remove (ema );

MessageBox. Show (string. Format ("{0} engineers in all departments", engineers. Count. Tostring ()));

SE leave = (SE) engineers [0];

MessageBos. Show (leave. SayHi ());

The RemoveAt () and Remove () methods can delete only one element.

Hashtable

Common methods and attributes of Hashtable

Attribute Name Description

Count: gets the number of key/value pairs contained in Hashtable.

Keys obtains the set contained in the Hashtable key.

Values obtains the set containing the Hashtable value.

Return Value Type Method Name Description

Void Add (Object key, Object value) adds elements with specified keys and values to Hashtable

Void Remove (Object key) Removes elements with specific keys from Hashtable

Void Clear () removes all elements from Hashtable

Hashtable adds elements through the Add () method

The Add () method of Hashtable has two parameters, one representing the key and the other representing the value not corresponding to the key.

Traverse elements in Hashtable

Foreach (Oject obj in engineers. Keys ){
Console. WirteLine (string) obj );
}

Foreach (Object obj in engineers. Values ){

SE se = (SE) obj;
Console. WriteLine (se. Name );
}

Generic and generic Sets

Generic

Generics introduce the concept of type parameters. By using type parameters (T), you can reduce the risk of forced conversion or packing during running. With generics, You can reuse code to the maximum extent to protect the security of types and improve performance, its most common application is to create a collection class, which can constrain the element types in the Collection class. Typical generic collections are List <T> and Dictionary <>;

List of generic sets <T>

Syntax

List <T> Object Name = new List <T> ();

Difference between List <T> and ArrayList

Similarities and Differences List <T> ArrayList

Different Points impose type constraints on saved elements to add any types.

You do not need to unpack the elements for adding/reading value types. You need to unpack the elements for adding/reading value types.

Similar points: accessing elements in a set through Indexes

The method for adding elements is the same.

The method for deleting elements is the same.

Generic set Dictionary <K, V>

Syntax

Dictionary <K, V> Object Name = new Dictionary <K, V> ();

For example:

Dictionary <string, SE> engineers = new Dictionary <string, SE> ();

Comparison between Dictionary and Hashtable

Similarities and Differences Dictionary <K, V> Hashtable

Different Points impose type constraints on saved elements to add any types.

You do not need to unpack the elements for adding/reading value types. You need to unpack the elements for adding/reading value types.

Same point, get Value through Key

The method for adding elements is the same.

The method for deleting elements is the same.

The Traversal method is the same.

Dictionary can also use the following method:

Foreach (KeyValuePair <string, SE> en in engineers ){
Console. WriteLine (en. Key );

Console. WriteLine (en. Value. Name );
}

Generic Type

Syntax

Public class name <T> {
//........
}

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.