Arrays
and
Data Structure Dr. Gamal Tharwat
ArrayLists
Chapter 2
[Link] 1
Arrays
Data Structure Dr. Gamal Tharwat
Chapter 2 - Part 1
[Link] 2
Introduction
• The array is the most common data structure,
present in nearly all programming languages.
• Using an array in C# involves creating an array
object of [Link] type, the abstract base
type for all arrays.
Data Structure Dr. Gamal Tharwat
• The Array class provides a set of methods for
performing tasks such as sorting and searching
that programmers had to build by hand in the
past.
3
ARRAY BASICS
• Arrays are indexed collections of data. The data
can be of either a built-in type or a user-defined
type.
• Arrays in C# are actually objects themselves
because they derive from the [Link] class.
Data Structure Dr. Gamal Tharwat
Since an array is a declared instance of the
[Link] class, you have the use of all the
methods and properties of this class when using
arrays.
4
Declaring and Initializing Arrays
type[] array-name;
Example :-
•string[] names
Data Structure Dr. Gamal Tharwat
•names = new string[10];
OR
string[] names = new string[10];
5
int[] numbers = new int[] {1,2,3,4,5};
Setting and Accessing Array Elements
• Elements are stored in an array either by
direct access or by calling the Array class
method SetValue.
1) Direct access involves referencing an array
Data Structure Dr. Gamal Tharwat
position by index on the left-hand side of an
assignment statement:
Example :-
names[2] = “Samir";
6
age[19] = 45;
Setting and Accessing Array Elements
2) The SetValue method provides a more
object-oriented way to set the value of an
array element. The method takes two
arguments, an index number and the value
Data Structure Dr. Gamal Tharwat
of the element.
Example :-
[Link][2, “Samir"];
7
[Link][19, 45];
Setting and Accessing Array Elements
• Array elements are accessed either by
direct access or by calling the GetValue
method.
• The GetValue method takes a single
Data Structure Dr. Gamal Tharwat
argument—an index.
Example :-
myName = names[2];
8
myAge = [Link][19];
Setting and Accessing Array Elements
• It is common to loop through an array in order to
access every array element using a For loop.
• A frequent mistake programmers make when
coding the loop is to either hard-code the upper
value of the loop (which is a mistake because the
Data Structure Dr. Gamal Tharwat
upper bound may change if the array is dynamic)
or call a function that accesses the upper bound
of the loop for each iteration of the loop:
Example :-
(for int i = 0; i <= [Link](0); i++) 9
totalSales = totalSales + sales[i];
Methods and Properties for Retrieving
Array Metadata
• The Array class provides several properties for
retrieving metadata about an array:
Returns the total number of
Data Structure Dr. Gamal Tharwat
Length elements in all dimensions of an
array
Returns the number of elements
GetLength in specified dimension of an
10
array.
Methods and Properties for Retrieving
Array Metadata
• The Array class provides several properties for
retrieving metadata about an array:
Returns the number of
Rank
Data Structure Dr. Gamal Tharwat
dimensions of an array.
Returns the Type of the
GetType
current array instance.
11
Methods and Properties for Retrieving
Array Metadata
• Example :-
Data Structure Dr. Gamal Tharwat
12
Multidimensional Arrays
• Multidimensional arrays are declared by
providing the upper bound of each of the
dimensions of the array.
• The two-dimensional declaration:
Data Structure Dr. Gamal Tharwat
int[ , ] grades = new int[4,5];
An array that consists of 4 rows and 5 columns
• You can also declare a multidimensional array
without specifing the dimension bounds.
double[ , ] Sales; 13
Multidimensional Arrays
• Declares a three-dimensional array.
double[ , , ] sales;
--------------------------------------
Data Structure Dr. Gamal Tharwat
• Multidimensional arrays can be initialized with
an initialization list.
14
Multidimensional Arrays
grade = Grades[2,2];
Grades(2,2) = 99
You can use the methods of the Array class:
grade = [Link][0,2]
Data Structure Dr. Gamal Tharwat
You can’t use the SetValue method with a
multidimensional array because the method
only accepts two arguments: a value and a
15
single index.
Multidimensional Arrays
Problem :
• if each row of the array is a student record,
we need to calculate the grade average for
each student
Data Structure Dr. Gamal Tharwat
16
Multidimensional Arrays
Data Structure Dr. Gamal Tharwat
17
Parameter Arrays
•When you want to write a method
definition that allows an optional
number of parameters. You can do this
using a construct called a parameter
Data Structure Dr. Gamal Tharwat
array.
•A parameter array is specified in the
parameter list of a method definition 18
by using the keyword ParamArray.
Parameter Arrays
Data Structure Dr. Gamal Tharwat
19
Jagged Arrays
• When you create a multidimensional array, you
always create a structure that has the same
number of elements in each of the rows.
Data Structure Dr. Gamal Tharwat
This array assumes each row (month) has The
same number of elements (days), when we know
that some months have 30 days, some have 31,
and one month has 29. With the array we’ve just
20
declared, there will be several empty elements in
the array.
Jagged Arrays
• The solution to this problem is to use a
jagged array instead of a two dimensional
array.
• A jagged array is an array of arrays where
Data Structure Dr. Gamal Tharwat
each row of an array is made up of an array.
Each dimension of a jagged array is a one
dimensional array.
• We call it a “jagged” array because the
number of elements in each row may be 21
different.
Jagged Arrays
int[ ][ ] jagged = new int[12][ ];
• The first set of parentheses indicates the row
number and the second set indicates the element
of the row array.
Data Structure Dr. Gamal Tharwat
• The first statement accesses
the first element of the first
array, the second element access the second
element of the first array, and the third statement 22
accesses the sixth element of the eighth array.
Jagged Arrays
Jagged Arrays
Data Structure Dr. Gamal Tharwat Data Structure Dr. Gamal Tharwat
24
23
ArrayLists
Data Structure Dr. Gamal Tharwat
Chapter 2 – Part 2
[Link] 25
The ArrayList Class
• Static arrays are not very useful when the size of
an array is unknown in advance or is likely to
change during the lifetime of a program.
-------------------
• One solution to this problem is to use a
Data Structure Dr. Gamal Tharwat
type of array that automatically resizes
itself when the array is out of storage space.
• This array is called an ArrayList and it is part
of the [Link] namespace in the 26
.NET Framework library.
Members of the ArrayList Class
• The ArrayList class includes several methods and
properties for working with ArrayLists.
Add() Adds an element to the ArrayList.
Data Structure Dr. Gamal Tharwat
Adds the elements of a collection to
AddRange() the end of the ArrayList.
Stores the number of elements the
Capacity ArrayList can hold. 27
Members of the ArrayList Class
• The ArrayList class includes several methods and
properties for working with ArrayLists.
Removes all elements from the
Clear() ArrayList.
Data Structure Dr. Gamal Tharwat
Determines if a specified item is in
Contains() the ArrayList.
Copies the ArrayList or a segment of
CopyTo()
it to an array. 28
Members of the ArrayList Class
• The ArrayList class includes several methods and
properties for working with ArrayLists.
Returns the number of
Count elements currently in the
Data Structure Dr. Gamal Tharwat
ArrayList.
Returns an enumerator to
GetEnumerator() iterate over the ArrayList.
Returns a subset of the
GetRange() 29
ArrayList as an ArrayList.
Members of the ArrayList Class
• The ArrayList class includes several methods and
properties for working with ArrayLists.
Returns the index of the first
IndexOf() occurrence of the specified
Data Structure Dr. Gamal Tharwat
item..
Insert an element into the ArrayList
Insert() at a specified index.
Inserts the elements of a collection
InsertRange() into the ArrayList starting 30
at the specified index..
Members of the ArrayList Class
• The ArrayList class includes several methods and
properties for working with ArrayLists.
Gets or sets an element at the
Item() specified index.
Data Structure Dr. Gamal Tharwat
Removes the first occurrence of the
Remove() specified item.
Removes an element at the
RemoveAt() specified index.
31
Members of the ArrayList Class
Reverses the order of the elements
Reverse() in the ArrayList.
Alphabetically sorts the elements in
Sort() the ArrayList.
Data Structure Dr. Gamal Tharwat
Copies the elements of the ArrayList
ToArray() to an array.
Sets the capacity of the ArrayList to
TrimToSize() the number of elements
in the ArrayList. 32
Using the ArrayList Class
ArrayList grades = new ArrayList();
Data Structure Dr. Gamal Tharwat
How to use a For Each loop with an
ArrayList?
33
Using the ArrayList Class Data Structure Dr. Gamal Tharwat
34
Using the ArrayList Class
Using the ArrayList Class
Data Structure Dr. Gamal Tharwat Data Structure Dr. Gamal Tharwat
36
35
Using the ArrayList Class
Using the ArrayList Class
Data Structure Dr. Gamal Tharwat Data Structure Dr. Gamal Tharwat
38
37
SUMMARY
•The array is the most
commonly used data
structure in computer
Data Structure Dr. Gamal Tharwat
programming.
•Most, if not all, computer
languages provide some type 39
of built-in array.