C # study notes three arrays, sets, and hash tables

Source: Internet
Author: User

Arrays can organize relevant data according to certain rules and quickly manage data through indexes or subscripts.

In addition, the arraylist and hashtable classes are provided in C #, which are set and hash tables respectively, and can also store multiple data.

 

1. One-dimensional array

1. Statement

Type [] arrayname;

Example: int [] intarr;

 

2. Initialization

Int [] arr = new int [5];

Int [0] = 1;

Int [1] = 2;

 

Initialization during declaration:

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

 

The array is declared without initialization, but the new operator must be used during initialization.

String [] strarr;

Strarr = new string [7] {"sun", "mon", "Tue", "wed", "Thu", "fri", "sat "};

 

During array initialization, the length of the new operator and array can be omitted. The Compiler automatically calculates the length of the array based on the number of initial values. This is not in conflict with the above

String [] strarr = {"sun", "mon", "Tue", "wed", "Thu", "fri", "sat "};

Example:

String [] strarr; <br/> strarr = new string [7] {"sun", "mon", "Tue", "wed", "Thu ", "fri", "sat" };< br/> foreach (string STR in strarr) <br/>{< br/> console. writeline (STR); <br/>}

 

2. Two-dimensional array

1. Statement

Type [,] arrayname;

For example, a two-dimensional array with two rows and two columns is generated.

Int [,] arr = new int [2, 2];

 

2. Initialization

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

Int [,] arr = new int [,] {1, 2}, {3, 4}; // No rows or columns are specified.

Example: declare an array of three rows and two columns and program to read its dimension.

Int [,] arr = new int [,] {1, 2}, {3, 4}, {5, 6 }};
Console. writeline ("number of rows: {0}", arr. getlength (0 ));
Console. writeline ("Number of columns: {0}", arr. getlength (1 ));

 

3. Dynamic Array

A dynamic array actually writes the definition part and initialization part of the array in different statements.

Type [] arrname;

Arrname = new type [N1, N2,...];

N1 and N2 can be Integer constants or variables in the array length. N1 is the first dimensional length, N2 is the second dimensional length.

Example

Int m = 2;

Int n = 3;

Int [,] arr2 = new int [M, N];

 

Iv. Array Operations

1. Traverse foreach

2. add and delete

Deleting is to advance the subsequent elements one by one. It is usually completed using arraylist.

For example, delete the second element.

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

Int I = 1; // second, followed by advance

For (INT I = N; I <arr. Length-1; I ++ ){

Arr [I] = arr [I + 1];

}

3. Method sort reverse

Static Method of sorting sort class.

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

Array. Sort (ARR );

Reverse the static method of the reverse class

 

V. arraylist set

The Set stores data in a linear structure. C # provides centralized collection classes, such as arraylist, quene, and stack.

The arraylist class is located in the system. Collections namespace. You can dynamically add and delete elements.

Arraylist is equivalent to an advanced dynamic array. It is an upgraded version of the array class, but not an array.

(1) The array capacity is fixed, and the arraylist capacity can be automatically expanded.

(2) arraylist provides methods to add, delete, and insert a range of elements, but array can only operate on one element at a time.

(3) arraylist provides the function of returning read-only and fixed-size packages to the set. arrays are not provided.

(4) arraylist can only be one-dimensional, while array can be multi-dimensional.

1. Three Constructors

(1) Public arraylist ();

Initializes the internal array with the default size of 16.

Arraylist Al = new arraylist ();

For (INT I = 0; I <10; I ++)

Al. Add (I );

(2) construct it using an icollection object and assign values to it using the elements of the set.

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

Arraylist Al = new arraylist (ARR); // array set

(3) initialize with the specified size

Arraylist Al = new arraylist (10 );

For (INT I = 0; I <Al. Count; I ++)

2. Common attributes

Capacity: gets or sets the number of elements that an arraylist can contain.

Count: Number of actually contained elements

Isfixedsize: Indicates whether a fixed size exists.

Isreadonly: Read-only or not

Issynchronized: whether to synchronize access to arraylist

Item: gets or specifies the element at the index.

Syncroot: Get the object that can be used to synchronize arraylist access.

3. Common Methods

1. Add add (object value)

2. insert (INT index, object value );

3. Delete

(1) Clear () Remove all elements

(2) Public Virtual void remove (Object OBJ );

Example: declare a one-dimensional array containing six elements, instantiate an arraylist object with this array, and then remove the elements matching 3 from the arraylist object using the Remove Method.

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

Arraylist Al = new arraylist (ARR );

Al. Remove (4 );

(3) removeat (INT index );

Deletes an element at the specified index location.

(4) removerange (INT index, int count)

Remove a certain range of elements

4. Traverse foreach

5. Search

(1) contains, bool type, determine whether to include the specified Element

(2) indexof returns the index of the specified element for the first time.

(3) lastindexof reverse search

 

6. hashtable hash table

Hashtable is used to represent a set of key-value pairs. These key-value pairs are organized according to the key hash code. Each element is a key-Value Pair stored in the dictionaryentry object. The key cannot be a null reference, and the value can be.

1. Constructor

(1) Public hashtable ()

Use the default initial capacity, default load factor, hash code provider, and comparator to initialize the new empty instance of the hashtable class.

(2) Public hashtable (INT capacity)

Specify initial capacity

 

2. Common attributes

Count: number of key-value pairs

Keys: gets the icollection containing the hashtable key.

Values: gets the icollection containing the hasntable value.

 

3. Common Methods

(1) Add add

Public Virtual void add (Object key, object value );

(2) Delete an element

Clear: Clear

Remove (Object key): deletes the key-value pair of the specified key.

(3) Traversal

Foreach, pay attention to the use of dictionaryentry type

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. collections; // you need to add system. collections namespace <br/> namespace consoleapplication1 <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> hashtable ht = new hashtable (); <br/> ht. add ("ID", "bh001"); // Add Method Add <br/> ht. add ("name", "TM"); <br/> ht. add ("sex", "male"); <br/> ht. add ("Age", "15"); <br/> console. writeline ("the number of elements in hashtable is:" + ht. count); // number <br/> ht. remove ("ID"); // remove <br/> foreach (dictionaryentry de in HT) // traverse the output. Note the type <br/>{< br/> console. writeline (de. value); // The dictionaryentry object represents a key-Value Pair <br/>}< br/> console. readline (); <br/>}< br/>

 

(4) Search

Public Virtual bool contains (Object key); whether the key is included, bool type

Public Virtual bool containsvalues (object value); whether to include a value

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.