C # Collection Types--array, ArrayList, List

Source: Internet
Author: User

In the study or work, the collection is often used, you can change the words "No Item no Collection", "Project has a collection." It typically stores a series of data or carries out a series of data related operations. Here is a brief discussion of some collection types related to the review.

Arrays (Array)

Arrays typically store only one type of data, one-dimensional arrays, two-dimensional arrays, and so on. are based on the System.Array class.

Format: type[] typename; type refers to types, typename index group names.

One-dimensional arrays have several forms of writing:

int [] a={1,2,3};

int [] B=new int[]{1,2,3};

int [] C; C=new int[]{1,2,3};

int [] d=new int[3];d [0]=1;d[1]=2;d[2]=3;

Two-dimensional arrays: equivalent to tables

int[,] e=new int[2,2]; e[0,0]=1;e[0,1]=2;e[1,0]=3;e[1,1]=4;//two rows and two columns

int[,] f=new int[2,2]{{1,2},{3,4}};//number of rows known

int[,] f=new int[,]{{1,2},{3,4}};//unknown number of rows, according to the following initialization to specify the number of rows

Three-dimensional analogy, equivalent to the Quartet body.

Jagged arrays: equivalent to each element of a one-dimensional array is an array. An array of arrays, often called.

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

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

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

int[][] k=new int[2][]; k[0]=new int[3]{2,4,6}; k[1]=new int[5]{1,2,3,4,5};

Then, k[0][0]=2;k[0][1]=4;k[1][2]=3;

int[][,] m=new int [2][,]

{New int[,] {{1,3},{5,7}},new int[,]{{0,2},{4,6}}}

Array Common operations

(1) iterating through an array

int [] a={1,2,3};

foreach (int n in a) {Console.WriteLine (n); }

Result Output 1 2 3

(2) converting between array strings

String[] sarr1={"123", "456", "789"};

String Str1=sarr1.join (",", sArr1);//The result is a string: 123,456,789

String[] Sarr2=str1. Split (', ');//The string is converted to a string array, removing the delimiter "," and converting the dimension array

-----------------------------------------------

String str2= "ABC 12,65;8"

string [] sarr2=str2. Split (', ', ', '; ');

----------------------------------------------

String s= "13MNNFSJ";

Char[] A=s.tochararray (); \ \ Converts the string to a char array.

(3) Common functions

Int[] a={1,2,3,4};

A.sum ();//sum; A.average ();   A,min (); A.max ();

(4) Sorting, copying, finding

int []a={1,2,3};

int []b=new int[a.length];

Array.copy (a,b,a.length);//Copy the A array to the B array

Array.Sort (a);//array A in ascending order

Array.reverse (a);//reverse Sort a array

Use the Contains method and the IndexOf method to find the specified element.

ArrayList class

Equivalent to an advanced dynamic array. Using System.Collections.Generic; it is more advanced than an array, can delete and add elements dynamically, and its capacity can be expanded, but it has only one-dimensional form. Generally there are three ways of construction.

One, the default size (16) to initialize

ArrayList arr=new ArrayList ();

Second, add a collection element to the ArrayList

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

ArrayList arr1=new ArrayList (a);

Three, specify the size to initialize,

ArrayList arr2=new ArrayList (+);

Common properties: Slightly, in VS, for example, enter the above arr2. The properties and descriptions are displayed.

Common methods

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

ArrayList arr1=new ArrayList (a);

(i) Increased

1,add (Element), method,

Arr1. ADD (7);//ARR1 element is 1 2 3 7

2,insert (index, Element); method//index starting from 0

Arr1. Insert (1,7);//arr1 element is 1 7 2 3

(ii) deletion

1,clear () method//Delete all

Arr1. Clear ();//arr1 is empty.

2.Remove (Match Object) method//Remove first match object

Arr1. Remove (3);//arr1 element is 1 2

3,removeat (Index) method//delete the element corresponding to the index

Arr1. RemoveAt (1);//arr1 element is 1 3

4,removerange (index, Range) method//delete a range of numbers from the index

Arr1. RemoveRange,//arr1 element is 1

(iii) traversal

Same as Array

(d) Find

Contains (Element);

Arr1. contain (3);//Determine if arr1 contains 3 of this element, contains, is true; not included as False

List

Using System.Collections.Generic; It is a mistake that ArrayList the type, improves the encoding quality, and is less of a type. Here is a brief explanation of the usage of the General list, which will be studied in depth later on in the use of generic lists.

List <int> lis1=new list<int> ();

(i) Add

Lis1. Add (500);//Adds a string of elements directly

List1. AddRange (New int[]{501,502});//Add a string of elements.

(b) Insert, Delete, find and so on a series of operations similar to the above ArrayList and array.

--------------------------------------------

The specified type can also be a custom type, such as:

1, foresight a person class

Class Person {
public string name;
public string Name {
Get{return name;}
set {value = name;}
}
}

2, create Person object

Person p = new person ();
P.name = "Chen";
person P1 = new person ();
P1.name = "Li";
person P2 = new person ();
P2.name = "Zhang";

3, create a collection of person types and put the Perosn object in the collection

List <person>li=new list<person> ();
Li.add (P);
Li.add (p1);
Li.add (p2);


Console.WriteLine (li[0].name);//The first element of the output collection, with the result: Chen.

C # Collection Types--array, ArrayList, List

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.