Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

SORT ARRAYS

method lets you sort elements array. You can use the

not only with single-

also with jagged arrays because more than one single-dimensional

the IComparable or IComparer in the array implements. The

interfaces are defaults that C# with the Array.Sort method, so

about programming the interface

When you sort an array, C# orders the elements in that array in alphabetical order for strings and in numerical order for numerical types. You can then tie in the sort to a Console.WriteLine statement as shown in the task example so you can see how C# will sort the arrays.

For example, if you have a set of strings as your elements, and you want to write your sorted elements to the screen, the output will show the string with the first letter closest to the letter a and continue on in the list. If you have a numeric list, then the first number in the output will be the one that has the lowest amount, even if that amount is a negative number.

SORT ARRAYS

Properties

Click Start Programs

 

 

 

 

 

The Start page appears.

The New Project window

Microsoft Visual Studio .NET

 

¤ Click New Project.

appears.

 

7.0 Microsoft Visual

 

 

 

Click the Console

Studio .NET 7.0.

 

 

 

 

 

Application icon in the

 

 

 

 

 

 

Templates pane.

Type a name for the file.

ˇ Click OK.

146

When you sort arrays that have strings that contain capital letters, C# considers those strings to be lower on the alphabetization list than strings with lowercase letters.

TYPE THIS:

using System; class SortArray;

{

public static void Main()

{

string[] names = {"too", "two", "To", "Too"}; Array.Sort(names);

foreach (string value in names)

{

Console.WriteLine("The word is {0}", value);

}

}

}

USING ARRAYS 7

RESULT:

The word is too

The word is two

The word is To

The word is Too

Á Type the code that creates an instance of your array, the elements in your array, the sort method, and outputs the results.

Type the Output method that outputs the information to the screen.

° Run the program by

· Save the program as the

pressing the F5 key.

filename.

The sorted array elements

 

with their associated array

 

locations appear on the

 

screen.

 

147

C#

SEARCH ARRAYS

C# lets you search for the first instance of an element in an array in case you need to pass a particular element in your array to another part of your

program or if you need to get some specific information such as finding the number of times an element appears in an array.

You can search within an array using the Array.IndexOf method. This built-in method returns the index number of the first array element that you want to search for. For example, if you search for the third element in an array, then the Array.IndexOf method returns the index

number 2 because the default first index number in an array is 0. If you set the first index number yourself, then the index number returned for your found element will vary.

The Array.IndexOf method also lets you search for an array element within certain index positions. For example, you can search for an array element that is the string and that appears between index number 2 and 10. You can also search for an array element from an index position through the last element in the array.

The drawback to using the Array.IndexOf method is that you can only search within a single-dimensional array.

SEARCH ARRAYS

Properties

 

 

 

 

 

 

 

The Start page appears.

The New Project window

.NET

 

¤ Click New Project.

appears.

 

 

 

 

 

Click the Console

 

 

 

 

 

 

 

Application icon in the

 

 

 

Templates pane.

Type a name for the file.

ˇ Click OK.

USING ARRAYS 7

You can use the Array.LastIndexOf method to find the last occurrence in an array.

TYPE THIS:

using System; public class Sample

{

public static void Main()

{

array Sample=Array.CreateInstance( typeof (String), 3); Sample.SetValue( "Five", 0 );

Sample.SetValue( "by", 1 );

Sample.SetValue( "Five", 2 ); string String1 = "Five";

int Index1 = Array.LastIndexOf( Index1, String1 );

Console.WriteLine("The last occurrence of \"{0}\" is at index {1}.", String1, Index1);

}

}

RESULT:

The last occurrence of “Five” is at index 2.

Á Type the code that creates an instance of your array, the elements in your array, the

Array.IndexOf search method, and outputs the results.

Type the Output method that outputs the information to the screen.

° Run the program by

· Save the program as the

pressing the F5 key.

filename.

The element and its

 

associated number appear

 

on the screen.

 

149

Соседние файлы в папке c#