Professional Documents
Culture Documents
1
Objectives
• How to declare an array and assign values to
array elements
• How to initialize an array
• How to use subscripts to access array elements
• How to use the Length field
• How to use foreach to control array access
2
Objectives
3
Objectives
4
Declaring an Array and Assigning Values to Array
Elements
• Sometimes storing just one value in memory at a time is not
adequate
• An array is a list of data items that all have the same type and
same name
• Example of array declaration:
double[] salesFigure;
• Like objects, memory is not actually reserved until the new
keyword is used
• Example: salesFigure = new double[20];
5
Declaring an Array and Assigning Values to Array
Elements
6
Declaring an Array and Assigning Values to Array
Elements
7
Initializing an Array
8
Using Subscripts to Access Array Elements
9
Using the Length Field
10
Using foreach to Control Array Access
11
Manually Searching an Array for an Exact Match
12
Manually Searching an Array for an Exact Match
13
The BinarySearch() Method
14
The BinarySearch() Method
15
Using the Sort() and Reverse() Methods
16
Using the Sort() and Reverse() Methods
17
Writing Methods That Accept Array Parameters
18
Using Parameter Arrays
19
Using Parameter Arrays
20
Declaring an Array of Objects
21
Using the BinarySearch() and Sort() Methods
with Object Arrays
• The use of methods like BinarySearch() and Sort() become
complicated when you use them with arrays of user-defined
objects
• When you create a class containing many fields, you must tell
the compiler which field to use when making comparisons
• An interface is a collection of methods that can be used by
any class, as long as the class provides a definition to override
the interface’s abstract definition
22
Using the BinarySearch() and Sort() Methods
with Object Arrays
• C# contains an interface named IComparable, which contains
the definition for the CompareTo() method that compares
one object to another and returns an integer
23
Using the BinarySearch() and Sort() Methods
with Object Arrays
• When you create a class whose members you will want to
compare, you must include two additional features in your
class:
– A single colon and the interface name IComparable after the class
name
– You must write a method containing the header
int IComparable.CompareTo(Object o)
24
Using the BinarySearch() and Sort() Methods
with Object Arrays
• The CompareTo() method must return an integer value
25
Using the BinarySearch() and Sort() Methods
with Object Arrays
26
Multidimensional Arrays
27
28
• Declaration
– int [,] myTable = new int[2,3];
• Set values
– myTable[0,1] =2;
– myTable[0,2] =12;
– myTable[1,O] =18;
– myTable[1,1] =74;
– myTable[1,2] =0;
• Instantiating and accessing Jagged Arrays
• A jagged may is one in which the length of each row is not the
same. For example we may wish to create a table with 3 rows
where the length of the first row is 3, the second row is 5 and the
third row is 2. We can instantiate this jagged may like this:
– int [] [] myTable = new int [3] [] ;
– myTable[O] = new int [3] ;
– myTable[1] = new int [5] ;
– myTable[2] = new int [2]; 29
Chapter Summary
30
Chapter Summary
31
Chapter Summary
32