Array Methods- I

Array Methods Used with the Array Class

Array.Clear(arrayName, startElement, Number to delete) method :

It provides deletion of the desired number of elements, starting from the desired element in the array. The number of elements of the array does not change. Only the values ​​of the specified array elements are deleted.

int[] class = new int [11];

Array.Clear(class, 3, 2);

In this example, starting from the 3rd element of the array, 2 elements will be deleted. So elements 3 and 4 of the array will be deleted.

 

Array.Reverse(arrayName) method:

Reverse array elements. (First element at the end, last element at the beginning)

int[] z = new int [11]; 

Array.Reverse(z);

In this example, the entire sequence is reversed. 

Array.Reverse(z, 5, 10);

When used as above, it will reverse 10 elements in total, starting from the 5th element of the array. Other elements will not be affected.

 

Array.Sort(arrayName, startElement, Number to Sort) method:

Sorts the elements of the array from smallest to largest (0-9 or less) . The method has two states. If only the string name is written in parentheses, it sorts the entire string. If desired, a certain part of the array can be sorted by giving the number of the starting element and the number of elements to be sorted.

string[] class = { "elin","ozge","merve","sultan","ceylan" };

Array.Sort(class)

In this example the entire array is sorted from smallest to largest (a – z).

string[] class = { "elin","ozge","merve","sultan","ceylan" };

Array.Sort(class, 2, 3);

In this example, the last three elements of the entire array are sorted.

 

Array.IndexOf(arrayName, SearchValue, startingElement, Number of Elements) method :

It allows searching the desired value among the elements of the array and giving the index number when found. If the search is not found, it returns -1.

The method has two states. If only the string name is written in parentheses, the search is done within the entire string. If desired, only a certain part of the array can be searched by giving the number of the starting element and the number of the element.

string[] friends = { "elin","ozge","merve","sultan","ceylan" };

Array.IndexOf(friends , "sultan");

In this example, the value "sultan" is searched in the entire array and the index number is found.

string[] friends  = { "elin","ozge","merve","sultan","ceylan" };

Array.IndexOf(friends , "sultan", 2, 3);

In this example, the value "sultan" is searched for 3 elements that follow, starting with the 3rd element of the array.

Note: The search process is case sensitive. For example, searching for "Ahmet" will not find the word "ahmet".

c# array methods, methods and explanations about arrays, sorting arrays, finding the largest and smallest item in the array, searching within the array, reversing the array

EXERCISES

There are no examples related to this subject.



COMMENTS




Read 793 times.

Online Users: 502



array-methods-used-with-array-class