10. C # Basic finishing (collection)

Source: Internet
Author: User

Collection 1, reference to collection
using System.Collections; // Add Class
2. Define the collection (ArrayList or Array)
New ArrayList ();
3, add data (can put a variety of different data types, but it is best to put only one) (a) Single data

Method 1:add adds

Arr. ADD (3); arr. ADD (5); arr. ADD ("hello");

Output Result:

Console.WriteLine ("add[0]:"+arr[0//  Generates a 0-based index by adding the element's order, reading the data through the index, and outputting the result as "3"Console.WriteLine ("add[1]:" + arr[1  ]);//5console.writeline ("add[2]:"+arr[2]);//hello

Method 2:Insert inserts to move data that starts from the data shown in the index one

Arr. Insert (1); // Insert (index, inserted data)

Output Result:

Console.WriteLine ("Insert[0]"+arr[0]);//3Console.WriteLine ("Insert[1]"+ arr[1]);// -Console.WriteLine ("Insert[2]"+ arr[2]);//5Console.WriteLine ("Insert[3]"+ arr[3]);//Hello
(b) Array:

1. Append a set of data addrange

int New int [3678  };arr. AddRange (Shuzu);

Traverse:

foreach (object in arr) // because there may be more than one data in the collection, the object (base class) is used to traverse, and all types can be converted to this type {Console.WriteLine (" Traverse 1:" + O);}

2. Insert a set of data starting at the specified index Insertrange

Arr. Insertrange (2, Shuzu); // Traverse foreach (object in arr) {Console.WriteLine (" Traverse 2:" + O);}
4. Remove the data: (only the first match is removed)

(a) removing the specified data (value): Remove

Arr. Remove (+); // removing data with a value of 17 foreach (object in  arr) {Console.WriteLine ("Remove:" + O);}

(ii) Remove the element at the specified index location: RemoveAt

Arr. RemoveAt (2); Console.WriteLine ("Remove after index is [3] data:"+arr[3]);
5. Sort:

(1) Sort: Automatically in ascending order

New ArrayList ();  for (int05; i++) {arr1. ADD (int. Parse (Console.ReadLine ()));} Arr1. Sort (); foreach (object in arr1) {Console.WriteLine (" ascending sort:"+ o);}

(2) Reverse: flips the sort of collection (makes it descending)

arr1. Reverse (); foreach (object in arr1) {Console.WriteLine (" flipped reverse after descending:"+O);}
6. Properties of the collection:

Count: Returns how many elements are inside the collection

Console.WriteLine (" number of elements:" + arr1. Count);
7. Return to index IndexOf
Console.WriteLine ("Hello's index is:"+arr. IndexOf ("hello")); // returns only the index of the first occurrence Console.WriteLine ("the last occurrence where Hello is located is atindex:" + arr.) LastIndexOf ("hello")); // returns the index in which the last occurrence is found

* * Inserted array index is also arranged by a single data, such as: Insert a set of three number of arrays, will establish three indexes.

8, contains: Determine if there is an element, return bool value

Arr. Contains (1);

9. Clear the collection

Arr. Clear ();

10. C # Basic finishing (collection)

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.