Emerging Technology
Chapter 8 { Arrays and Collections }
Usman Akbar
[email protected]
FB Group - SuperiorUniversity2016
1
Chapter Objectives
Learn array basics
Declare arrays and perform compile-time initialization of array elements
Access elements of an array
Become familiar with methods of the Array class
Write methods that use arrays as parameters
Write classes that include arrays as members and instantiate user-defined array
objects
2
Chapter Objectives (continued)
Create two-dimensional arrays including rectangular and jagged types
Use multidimensional arrays
Use the ArrayList class to create dynamic lists
Learn about the predefined methods of the string class
Be introduced to the other collection classes
Work through a programming example that illustrates the chapter’s
concepts
3
Array Basics
An array is a group of variables—called elements—
containing values of the same type
Data structure that may contain any number of variables
Variables must be of same type
Single identifier given to entire structure
Individual variables are called elements
Elements accessed through an index
Index also called subscript
Elements are sometimes referred to as indexed or subscripted variables
4
Array Basics (continued)
Arrays are objects of System.Array class
Array class includes methods and properties
Methods for creating, manipulating, searching, and sorting arrays
Creating an array is the same way, you instantiate an object of a user-defined
class
Use the new operator
Specify number of individual elements
5
Array Declaration
Format for creating an array
type [ ] identifier = new type [integral value];
Type can be any predefined types like int or string, or a class that you create in
C#
Integral value is the number of elements
Length or size of the array
Can be a constant literal, a variable, or an expression that produces an integral value
6
Array Declaration (continued)
Figure 7-1 Creation of an
array
7
Array Declaration (continued)
Array identifier, name, references first element
Contains address where score[0] is located
First index for all arrays is 0
Last element of all arrays is always referenced by an index with a value of the
length of the array minus one
Can declare an array without instantiating it
The general form of the declaration is:
type [ ] identifier;
Example int[ ] score = new int[10];
8
Array Declaration (continued)
Figure 7-2 Declaration of an array
9
Array Declaration (continued)
General form of the second step is:
identifier = new type [integral value];
Examples
const int size = 15;
string [ ] lastName = new string [25];
double [ ] cost = new double [1000];
double [ ] temperature = new double [size];
int [ ] score;
score = new int [size + 15];
Two steps
10
Array Initializers
Compile-time initialization
General form of initialization follows:
type[ ] identifier = new type[ ] {value1, value2, …valueN};
Values are separated by commas
Values must be assignment compatible to the element type
Implicit conversion from int to double
Declare and initialize elements in one step
11
Array Initializers (continued)
Array length determined by number of initialization values placed inside curly
braces
Examples
int [] anArray = {100, 200, 400, 600};
char [ ] grade = new char[ ] { ‘A’, ‘B’, ‘C’, ‘D’, ‘F’};
double [ ] depth = new double [2] {2.5, 3};
No length specifier is required
12
Array Initializers (continued)
Figure 7-3 Methods of creating and initializing arrays at compile time 13
Array Access
Specify which element to access by suffixing the identifier with an index
enclosed in square brackets
score[0] = 100;
Length – special properties of Array class
Last valid index is always the length of the array minus one
14
15
Output
16
Calculating a Value to Store in Each Array Element
17
Output
18
Summing the Elements of an Array
19
Iterating Through Arrays with foreach
Used to iterate through an array
Read-only access
General format
foreach (type identifier in expression)
statement;
Identifier is the iteration variable
Expression is the array
Type should match the array type
20
Array with FOREACH Loop
21
Using foreach with Arrays (continued)
string [ ] color = {"red", "green", "blue"};
Displays red,
foreach (string val in color) blue, and green
Console.WriteLine (val); on separate lines
Iteration variable, val represents a different array element with each loop
iteration
No need to increment a counter (for an index)
22
Questions …?
67