(C # Array of teaching ideas) common methods for arrays An example, review array

Source: Internet
Author: User
Tags foreach array length arrays

In this section we use arrays of common methods to manipulate the array, while leaving a few arrays of exercises to review the array.

Common methods of arrays

I mentioned in the previous section that the method is the function of the object, usually verbs, such as cleaning cloth to remove dust, clear is its function, the same array has a variety of functional methods, such as emptying, searching, sorting, and so on, these three methods is the focus of this section, I will explain each one. First, let's Learn

An array of empty array.clear (arrayname, int index, int length);

(The array name of the operation, the starting index, the length of the element being cleared)

Look at the example below

int[] arr = new int[] {1,2,3,4,5};
foreach (int i in arr)
{
Console.WriteLine(i);
}
Array.Clear(arr, 1, 2); 
Console.WriteLine (“从1号索引开始清除2个元素后的数组长度"+arr .Length );
foreach (int i in arr)
{
Console.Write(i+" ");
}

Question: Students think about the results of the output what it looks like? As a result: the array length after clearing 2 elements from the 1th index is 5

1 0 0 4 5.

And not 1 4 5. What is the reason? This is because of the array's attributes, which have been determined once the size of the array has been initialized, so that the emptying of the array elements is set back to 0, false or null, which is related to the type of the array elements, but the length of the arrays remains unchanged.

Next, we'll learn the array lookup

Array.indexof (array,obj); Find the index of the first OBJ element that appears in the ARR array

Array.lastindexof (arr,obj); Find the arr index of the last occurrence of the obj element in the array

int[] arr = new int[] { 3, 2, 1, 5,2 ,4};
foreach (int i in arr)
{
Console.WriteLine(i);
}
int z=Array.IndexOf(arr, 2);
int j=Array.LastIndexOf(arr,2);
Console.Write("第一次出现2的索引位:"+z);
Console.WriteLine("最后一次出现2的索引位:" +j);

The result is the first occurrence of 2 index bits: 1 The last occurrence of 2 index bits: 5 The results are consistent with the facts, and the Array.indexof and Array.lastindexof methods provided by C # also provide two methods:

Array.indexof (Array,obj,beginindex), the index bit of the start query.

Array.indexof (array,obj,beginindex,count); the index bit of the starting query, and the index adds the number of queries to the larger direction.

Array.lastindexof (Array,obj,beginindex), the index bit of the start query.

Array.lastindexof (array,obj,beginindex,count); the index bit of the starting query, and the index adds the number of queries to the small direction.

And then the example above

Console.WriteLine ("The index bit of the first occurrence of 2 in 4 elements of a large index starting at the number 1th index:" + b);

Console.WriteLine ("The index bit of the last occurrence of 2 in 4 elements from the number of index bits starting at 4th": + L);

The results of the operation are:

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.