CSC 220 Data Structures and Algorithms: Lecture # 3
CSC 220 Data Structures and Algorithms: Lecture # 3
CSC 220
Data Structures and Algorithms
Lecture # 3
Arrays
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Lecture Outlines 2
Lecture Outlines
• Overview of Arrays
What is an Array?
Array Notation in C#
Array Rank
Accessing Array Elements
Checking Array Bounds
Comparing Arrays to Collections
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Lecture Outlines 3
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Lecture Outlines 4
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Overview of Arrays 5
Overview of Arrays
• What is an Array?
• Array Notation in C#
• Array Rank
• Accessing Array Elements
• Checking Array Bounds
• Comparing Arrays to Collections
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
What is an Array? 6
What is an
Array?
• An array is a sequence of homogeneous elements.
All elements in an array have the same type.
Individual elements are accessed using integer indexes.
• Structs can have elements of different types.
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Array Notation in C# 7
Array Notation in C#
• You declare an array variable by specifying:
The element type of the array.
The rank of the array.
The name of the array variable.
type[ ] name;
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Array Rank 8
Array Rank
• Rank: the number of indexes associated with each element.
• Rank is also known as the array dimension.
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Accessing Array Elements 9
Accessing Array
Elements
• Supply an integer index for each rank.
• Indexes are zero-based.
2
3
1
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Checking Array Bounds 10
Checking Array
Bounds
• All array access attempts are bounds checked.
A bad index throws an IndexOutOfRangeException.
row grid
grid.GetLength(0)==2
row.GetLength(0)==6
grid.GetLength(1)==4
row.Length==6
grid.Length==2*4
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Comparing Arrays to Collections 11
Arrays drawbacks
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Creating Arrays 12
Creating Arrays
• Creating Array Instances
• Initializing Array Elements
• Initializing Multidimensional Array Elements
• Creating a Computed Size Array
• Copying Array Variables
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Creating Array Instances 13
Creating Array
Instances
• Declaring an array variable does not create an array.
• You must use new to explicitly create the array
instance.
• Array elements have an implicit default value of zero.
row
long[ ] row = new long[4]; 0 0 0 0
Variable Instance
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Initializing Array Elements 14
Initializing Array
Elements
• The elements of an array can be explicitly initialized.
• You can use a convenient shorthand.
1
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Initializing Multidimensional Array Elements 15
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Creating a Computed Size Array 16
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Copying Array Variables 17
Copying Array
• Copying an Variables
array variable copies the array variable only.
It does not copy the array instance.
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Using Arrays 18
Using Arrays
• Array Properties
• Array Methods
• Returning Arrays from Methods
• Passing Arrays as Parameters
• Command-Line Arguments
• Demonstration: Arguments for Main
• Using Arrays with foreach
• Quiz: Spot the Bugs
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Array Properties 19
Array Properties
0
int[,] grid = new int[2,3];
0 grid.Rank 2
0 0 0 grid.Length 6
grid 0
row 0 0 0
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Array Methods 20
Array Methods
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Returning Array from Methods 21
class Example {
static void Main( ) {
int[ ] array = CreateArray(42);
...
}
static int[ ] CreateArray(int size) { int[ ] created = new
int[size]; return created;
}
}
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Passing Arrays as Parameters 22
Passing Arrays as
Parameters
• An array parameter is a copy of the array variable.
Not a copy of the array instance.
class Example2 {
static void Main( ) {
int[ ] arg = {10, 9, 8, 7}; Method(arg);
System.Console.WriteLine(arg[0]);
}
static void Method(int[ ] parameter) { parameter[0]++;
}
} This method will modify
the original array
instance created in
Main
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Command-Line Arguments 23
Command-Line
Arguments
• The runtime passes command line arguments to Main.
Main can take an array of strings as a parameter.
class Example3 {
static void Main(string[ ] args) {
for (int i = 0; i < args.Length; i++) {
System.Console.WriteLine(args[i]);
}
}
}
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Using Arrays with foreach 24
class Example4 {
static void Main(string[ ] args) { foreach (string
arg in args) {
System.Console.WriteLine(arg);
}
}
}
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
?How to delete an element from array
25
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
array 2 7 9 12 15 23 30 45 70 77 80 88 90 99
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
array 2 7 9 12 15 23 30 45 70 77 80 88 90 99
Null
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
array 2 7 9 12 23 30 45 70 77 80 88 90 99 99
How to delete an element from array?
26
9. Console.Read();
10. }
How to delete an element from array?
27
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
array 2 7 9 12 15 23 30 45 70 77 80 88 90 99
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
array 2 7 9 12 13 15 23 30 45 70 77 80 88 90 99
How to insert a new element in array?
29
int [ ] array;
1 array = {0, 2, 4, 6};
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)