Array and set (C #)

Source: Internet
Author: User
1. array Overview

An array contains several variables of the same type, which can be accessed through indexes. The variables in an array are called the elements of an array. The number of elements that an array can contain is called the length of an array. Each element in the array has a unique index corresponding to it.

2. Declaration and use of one-dimensional arrays

 

2.1 statement:

Basic Syntax:

Type[]arrayName;

Sample Code:Declare an int-type one-dimensional array arr

Int [] arr; arr = new int [5] {1, 2, 3, 4, 5}; // initialize the elements 1, 2, 3, 4, and 5 respectively.
2.2 usage:

Sample Code:

Static void main (string [] ARGs) {int [] arr = {1, 2, 3, 4, 5}; // defines a one-dimensional array, and assign it foreach (int n in ARR) // use the foreach statement to traverse the element console in one-dimensional array cyclically. writeline ("{0}", N + ""); console. readline ();}

Running result:

3. Declaration and use of two-dimensional arrays

 

3.1 statement:

Basic Syntax:

type[ ,] arrayName;

Sample Code:Declare a two-dimensional array with two rows and two columns and initialize

  int[ , ]arr=new int[2,2]{{1,2},{3,4}};
3.2 use a static two-dimensional array 3.2.1

Sample Code:

Static void main (string [] ARGs) {int [,] arr = new int [2, 2] {1, 2}, {3, 4 }}; // customize a two-dimensional array console. write ("the number of rows in the array is:"); console. write (ARR. rank); // obtain the number of rows in the two-dimensional array console. write ("\ n"); console. write ("the number of columns in the array is:"); console. write (ARR. getupperbound (ARR. rank-1) + 1); // obtain the number of columns in the array console. write ("\ n"); For (INT I = 0; I <arr. rank; I ++) // cyclically outputs each element in a two-dimensional array {string STR = ""; for (Int J = 0; j <arr. getupperbound (ARR. rank-1) + 1; j ++) {STR = STR + convert. tostring (ARR [I, j]) + "";} console. write (STR); console. write ("\ n");} console. readline ();}

Running result:

3.2.2 dynamic two-dimensional array

Sample Code:

Static void main (string [] ARGs) {console. write ("Enter the number of rows in the dynamic array:"); int ROW = convert. toint32 (console. readline (); // defines the number of rows in the dynamic array console. write ("Enter the number of columns in the dynamic array:"); int Col = convert. toint32 (console. readline (); // defines the number of columns in a dynamic array int [,] arr = new int [row, Col]; // defines the console of a dynamic array based on the number of defined rows and columns. writeline ("Result:"); For (INT I = 0; I <row; I ++) {for (Int J = 0; j <Col; j ++) {console. write (I + J. tostring () + ""); // outputs the elements in the dynamic array} console. writeline (); console. readline ();}}

Running result:

4. Basic operations on Arrays

 

4.1 how to traverse Arrays

You can use the foreach statement to traverse arrays. The sample code of 2.1 already exists, so this is not an example here.

4.2 Add/delete array elements

You can use the index number of an array to delete array elements. However, this method cannot delete array elements. It is generally not recommended. After learning the arraylist class, you can use it to delete arrays.

4.3 sort arrays (a separate chapter will be available in the future for detailed explanation) 4.3.1 traversal sorting method: for example, the bubble method, direct insertion method, and select sorting method. 4.3.2 array-class sort and reverse sorting method 4.4 merge and split arrays 5. arraylist class

The arraylist class is equivalent to an advanced dynamic array, which is an upgraded version of the array class. Located in the system. Collections namespace, You can dynamically add or delete elements.

5.1 statement

Method 1: arraylist list = new arraylist ();

Sample Code:

Arraylist list = new arraylist (); // declare an arraylist object for (INT I = 0; I <10; I ++) // Add 10 int-type element values to the list. add (I );

Method 2: arraylist list = new arraylist (arrayname );

Sample Code:

Int [] arr = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9}; // declare a one-dimensional array arraylist list = new arraylist (ARR ); // Add the elements in the one-dimensional array to the declared arraylist object

Method 3: arraylist list = new arraylist (N );

Sample Code:

Arraylist list = new arraylist (10); // declare an arraylist object for (INT I = 0; I <list. count; I ++) // Add 10 int-type element values to the list. add (I );
5.2 use the 5.2.1arraylist element to add

You can use the ADD and insert methods provided by the arraylist class.

Sample Code:

// Use the add method (add the object to the end of the arraylist set) int [] arr = new int [] {1, 2, 3, 4, 5, 6 }; arraylist list = newarraylist (ARR); // instantiate an arraylist Object List using a declared one-dimensional array. add (7); // Add an object to the arraylist object // use the insert method (add the object to the specified index of the arraylist set) int [] arr2 = new int [] {1, 2, 3, 4, 5, 6}; arraylist list2 = newarraylist (arr2 ); // instantiate an arraylist Object List using a declared one-dimensional array. insert (); // Add an element at the specified position in the arraylist set
5.2.2 delete an arraylist Element

You can use the clear, remove, removeat, and removerange methods provided by the arraylist class. The usage of these methods is similar to that of adding elements in 5.2.1. You only need to replace the method name, so we will not give an example.

The clear method is used to remove all elements from the arraylist. The remove method removes the first matching item of a specific object. The removeat method is used to remove the elements at the specified index; the removerangge method is used to remove a certain range of elements. You can choose to use it as needed.

5.2.3 search for the arraylist Element

The element search is similar. You can use the contains, indexof, and lastindexof methods.

6. hashtable (hash table)

A hash table is a set of key/value pairs. These key/value pairs are organized according to the hash code of the key.

6.1 add a hashtable Element
Hashtable = newhashtable (); hashtable. Add ("sex", "male ");

6.2 delete the hashtable Element

        hashtable.Remove(“Sex”);
6.3 traversal of hashtable Elements

Similar to array traversal, you can use the foreach statement. However, because the hashtable element is a key/value pair, you need to use the dictionaryentry type for traversal.

Sample Code:

Static void main (string [] ARGs) {hashtable = new hashtable (); // instantiate hashtable object hashtable. add ("stuid", "110"); // Add the hashtable object to the hashtable object. add ("name", "Tom"); hashtable. add ("sex", "male"); console. writeline ("\ t key \ tvalue"); foreach (dictionaryentry dicentry inhashtable) // traverses the elements in hashtable {console. writeline ("\ t" + dicentry. key + "\ t" + dicentry. value);} console. writeline (); console. readline ();}


Running result:

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.