Array and ArrayList in C #

Source: Internet
Author: User
Tags foreach arrays instance method sort

Some methods of array

Int[] Nums = {5, 4, 6, 3, 14, 9, 8, 17, 1, 24,-1, 0};

Console.Write ("Order before Ordering:");

foreach (int i in nums)

Console.Write (i + ""); Console.WriteLine ();

int before = Array.indexof (Nums, 14);

Console.WriteLine ("Before sort index is" + before);

Ordinal number in the array before sorting

Array.Sort (nums);//static methods sort by array

Console.Write ("Order after Ordering:");

foreach (int i in nums) Console.Write (i + "");

Output array int index = Array.BinarySearch (nums, 14);

The ordinal number of the lookup Console.WriteLine ("The index is" + index);

Console.WriteLine (); string[] Strarray = new string[]

{"75.3", "25.999", "105.25"};

Type conversion double[] Doublearray = Array.convertall

<string, Double> (Strarray, convert.todouble);

Conversion of implementation types

Console.Write ("Converted to doubles:");

Array.foreach<double> (Doublearray, delegate (double x)

{Console.Write (x + "");}); /Delegate Events

Screenshot of program results

Second, ArrayList

Array is a static array, and once the size of the arrays is determined in the initialization process, it cannot be modified at the end, and the elements in the arrays cannot be added and deleted. This is not very flexible, for this C # provides ArrayList for handling dynamic arrays.

ArrayList after the creation can be based on the actual needs of the element additions and deletions. This is very useful. Let's look at the next ArrayList constructor.

ArrayList al = new ArrayList ();

Let's look at a simple example:

ArrayList al = new ArrayList ();

Console.WriteLine

("Initial Number of elements:" + al. Count);

Console.WriteLine ("Adding 6 elements");

ADD elements to the array list

Al. ADD (' C ');

Al. ADD (' A ');

Al. ADD (' E ');

Al. ADD (' B ');

Al. ADD (' D ');

Al. ADD (' F ');

Al. Remove (' a ');//Delete an element

Console.WriteLine (al. Count);

Al. Add (' a ');//Add an element

Console.WriteLine (al. Count);

Al[0] = ' X ';//modify array element

Al[1] = ' Y ';

AL[2] = ' Z ';

After you delete an element, the number becomes 5, and the number of elements added is 6.

So now let's build a digital ArrayList to learn how to do it;

ArrayList al = new ArrayList ();

ADD elements to the array list

Al.  ADD (155); Al. ADD (413);

Al.  ADD (-41); Al. ADD (818);

Al. ADD (31); Al. ADD (191);

Console.Write ("Original contents:");

foreach (int i in AL)

Console.Write (i + "");

Console.WriteLine ("\ n");

Sort sorting

Al. Sort ();

Console.Write ("Contents after sorting:");

foreach (int i in AL)

Console.Write (i + "");

Console.WriteLine ("\ n");

Console.WriteLine ("Index of 413 is" +

Al. BinarySearch (413));

The above figure is the result of the output, this time to ArrayList only made a simple introduction, the next to ArrayList provided by the instance method and static method to do a detailed introduction.

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.