Using Arrays
1
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
How to manually search an array to find
an exact match
How to search an array to find a range
match
How to use the BinarySearch() method
How to use the Sort() and Reverse()
methods
3
How to pass an array to a method
How to use parameter arrays
How to declare an array of objects
How to use the BinarySearch() and Sort()
methods with object arrays
4
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
A subscript or index is an integer contained
within square brackets that indicate the position
of one of an array’s variables, or elements
An array’s elements are numbered beginning
with 0
A common mistake is to forget that the first
element in an array is element 0
6
Output of the ArrayDemo1 program
7
Arrays, like object fields, have default values
You can assign nondefault values to array
elements upon creation
Examples:
int[] myScores = new int[5] {100,76,88,100,90};
int[] myScores = new int[] {100,76,88,100,90};
int[] myScores = {100,76,88,100,90};
8
The power of arrays become apparent when you
begin to use subscripts that are variables rather
than constant values
Example:theArray[sub] vs. theArray[1]
A loop can be used to cycle through the elements
of an array
Through the use of loops and arrays, code can
become more efficient
9
The subscript used to access an array must be
between the range of 0 to Length-1
Because every array is automatically a member
of the class System.Array, you can use the
fields and methods that are part of the
System.Array class
The Length() field is a member of the
System.Array class
10
C# supports a foreach statement that you can
use to cycle through every array element without
using subscripts
With the foreach statement, the programmer
provides a temporary variable that automatically
holds each array value in turn
11
One way to determine if some variable holds one
of many possible valid values is to use a series of
if statements
Instead of creating a long series of if statements,
a more efficient solution is to compare the
variable against the items in an array
In certain situations, where arrays are involved, it
might be a good idea to use parallel arrays
12
Accessing information in parallel arrays
13
The BinarySearch() method finds a requested
value in a sorted array
This method accepts two arguments: an array
and the field to be searched for
The method returns –1 if the value is not found in
the array, otherwise it returns the index where
the value is located
14
This method does NOT work under the
following conditions:
If the array items are not arranged in
ascending order, the BinarySearch() method
does not work correctly
If the array holds duplicated values, then the
BinarySearch may not work
If you want to find a range match rather that an
exact match, the BinarySearch() method does
not work
15
The Sort() method arranges array items in ascending order
16
The Reverse() method reverses the order of items in an array
17
When you pass an array to a method, changes
you make to array elements within the method
are permanent
Arrays, like all objects, are passed by reference
Within the method header, a parameter is
declared as an array using square brackets after
the argument type
18
When you don’t know how many arguments you
might eventually send to a method, you can
declare a local array within the method header
using the keyword param
For example:
public static void DisplayStrings (param string[]
people)
19
ParamsDemo program and the output
20
You can declare arrays that hold elements of
objects
To use a method that belongs to an object that is
part of an array, insert the appropriate subscript
notation after the array name and before the dot-
method
Example:
empArray[x].SetId(999)
empArray[x].SetSalary(7.25)
21
You can pass a single array element to a method
in exactly the same manner as you would pass a
variable. Alternatively, instead of passing a single
array element to a method, you can pass an entire
array.
When you don’t know how many arguments you
might eventually send to a method, you can
declare a local array within the method header by
using the keyword params
Just as you can declare arrays of integers or
doubles, you can declare arrays that hold
elements of any type
When you create a class containing fields, you
must create an IComparable interface containing a
CompareTo() method
22
An array is a list of data items, all of which have
the same type and the same name
In C#, arrays are objects of a class named
System.Array; like all objects, their fields are
initialized to default values
The power of arrays becomes apparent when you
begin to use subscripts that are variables rather
than constant values
When you work with array elements, you must
ensure that the subscript you use remains in the
range 0 through length -1
23
You can use the foreach statement to cycle
through every array element without using
subscripts
When you want to determine whether some
variable holds one of many possible valid values,
you can compare the variable to a list of values
in an array
You can create parallel arrays to more easily
perform a range match
The BinarySearch() method finds a requested
value in a sorted array
The Sort() method arranges array items in
ascending order. The Reverse() method reverses
the order of items in an array.
24
25
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
26
C# contains an interface named IComparable,
which contains the definition for the CompareTo()
method that compares one object to another and
returns an integer
27
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)
28
The CompareTo() method must return an integer
value
29
IComparable.CompareTo() method for Employee class
30