2014-12-18 Collection

Source: Internet
Author: User

First, define a new array class

Class Arraylistjihe
    {
        static void Main ( string[] args
        {
             ArrayList arr = new ArrayList ();
            arr. ADD (3);    
            arr. ADD ("Hello");
            // Generate a 0-based index by adding an element's order, reading the data through an index
             Console.WriteLine (arr[0]);
            Console.WriteLine (arr[2]);
       }
   }

ArrayList must refer to using System.Collections;

Arr. Insert (1,17) //17 This number is inserted at the position of the collection 1

Int[] Shuzu=new int[3]{6,7,8};

Arr.   Insertrange (1,shuzu); //split coefficient group, insert array to 1 position

Arr.  AddRange (Shuzu); //detach the coefficient group, append the array to the position following the collection

Show all content in a collection

foreach (Object o in arr)
{
Console.WriteLine (O.tostring ());
}

Adding an array entity
Arr. ADD (Shuzu);
int [] newshu= (int[]arr[3]);
foreach (int a in Newshu)
{
Console.WriteLine (a);
}

removing data
Arr.  Remove ("Hello"); //Remove the Hello from the first position
Arr.   RemoveAt (3); //Remove data at the specified location

Sorting functions for collections
ArrayList arr = new ArrayList ();
for (int i = 0; i < 5; i++)
{
Arr. ADD (int. Parse (Console.ReadLine ()));
}
Arr.  Sort (); //Ascending sort
Arr.  Reverse (); //Flip the collection

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

Console.WriteLine (arr.  Count); //Returns the number of elements in the collection
Console.WriteLine (arr. IndexOf (14)); //Returns the position of the first 14 in a collection
Console.WriteLine (arr. LastIndexOf (14)); //Returns the position of the last 14 in the collection

2014-12-18 Collection

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.