INF210 Lec3 Data Structures
INF210 Lec3 Data Structures
INF 210
Fall 2022-2023
Lecture 3
One & Two -Dimensional Arrays
INSTRUCTOR
DR/ AYMAN SOLIMAN & DR/ OSAMA FAROUK
Ø Contents
1) Introduction 9) 2-Dimensional Arrays
2) Array Basics 10) Arrays as a Data Structure – Accessing
3) Array Names 11) Arrays as a Data Structure - Searching
4) Parallel Arrays 12) Arrays as a Data Structure - Inserting
5) Array Types and size 13) Arrays as a Data Structure - Deleting
6) Creating Arrays 14) Pros and Cons
7) Numerical Indexes
8) Replacing information in an Array
§ But today we’ll be covering the Array more in-depth as a data structure
Elements
1 2 3 4 5 6 7 8 9 10
Array
1 2 3 4 5 6 7 8 9 10
§ A Name
§ A Type
§ A Size
Right 5 2
§ Represents the total amount of elements that can be stored within the array
§ Cannot be changed
34 ...
35 Define an array
§ A Name
§ A Type
§ A Size
§ A Name
§ A Type
§ A Size
§ A Name
§ A Type
§ A Size
Grocery List Eggs Milk Meat Grocery List Eggs Milk Meat
Grocery List Eggs Milk Meat Grocery List Eggs Milk Meat
Salaries 1 2 3 4 5 6 7 8 9 10
Salaries 10K 15K 13K 11K 25K 13K 13K 20K 15K 8K
int array[] = { 1, 2, 3 };
array = [1, 2, 3]
int[] array = { 1, 2, 3 };
int array[] = { 1, 2, 3 };
array = [1, 2, 3]
int[] array = { 1, 2, 3 };
int array[] = { 1, 2, 3 };
array = [1, 2, 3]
int[] array = { 1, 2, 3 };
int array[] = { 1, 2, 3 };
array = [1, 2, 3]
int[] array = { 1, 2, 3 };
int array[] = { 1, 2, 3 };
array = [1, 2, 3]
int[] array = { 1, 2, 3 };
int array[] = { 1, 2, 3 };
array = [1, 2, 3]
int[] array = { 1, 2, 3 };
int array[] = { 1, 2, 3 };
array = [1, 2, 3]
int[] array = { 1, 2, 3 };
int array[] = { 1, 2, 3 };
array = [1, 2, 3]
int[] array = { 1, 2, 3 };
int array[] = { 1, 2, 3 };
array = [1, 2, 3]
int[] array = { 1, 2, 3 };
int array[] = { 1, 2, 3 };
array = [1, 2, 3]
int[] array = { 1, 2, 3 };
int array[] = { 1, 2, 3 };
array = [1, 2, 3]
int[] array = { 1, 2, 3 };
int array[] = { 1, 2, 3 };
array = [1, 2, 3]
int[] array = { 1, 2, 3 };
Size = 3
int array[] = { 1, 2, 3 };
array = [1, 2, 3]
int[] array = { 1, 2, 3 };
Size = 4
int array[] = { 1, 2, 3, 4 };
array = [1, 2, 3, 4]
int[] array = { 1, 2, 3, 4 };
User Info 1 2 3 4 5 6 7 8 9 10
The information varies based on which user runs the code/what they input
Index
Numbers
1 2 3 4 5 6 7 8 9 10
Index 0
Numbers
1 2 3 4 5 6 7 8 9 10
Index 0 1
Numbers
1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Ø We reference it using both the arrays name and index number of the element
you wish to retrieve
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
If we want to deal with the 5th element -à deal with index number 4th
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
print(Numbers)
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
print(Numbers[4])
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
print(Numbers[10])
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
print(Numbers[10])
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Numbers[9]
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Numbers[9]=11
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Numbers[9]=11
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 11
Numbers[9]=11
Index 0 1 2 3 4 5 6 7 8 9
Numbers
1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4
Array
Index 0 1 2 3 4
Chess board
10/19/22 Dr/ Ayman Soliman 91
q The Array - 2-Dimensional Arrays
Ø An array with an array at each index is known as a 2-dimensional array
Index 0 1 2 3
Index 0 1 2 3
Index 0 1 2 3
Index 0 1 2 3
Index 0 1 2 3
Index 0 1 2 3
Index 0 1 2 3
Index 0 1 2 3
Index 0 1 2 3
Index 0 1 2 3
Memory
Spot in
Memory
0 1 2 3 4 5 6 7
Memory
int String
Type
Stored
Value
78 “Hey”
Spot in
Memory
0 1 2 3 4 5 6 7
Memory
int String
Type
Stored
Value
78 “Hey”
Spot in
Memory
0 1 2 3 4 5 6 7
Memory
int String
Type
Stored
Value
78 “Hey”
Spot in
Memory
0 1 2 3 4 5 6 7
Memory
int String
Type
Stored
Value
78 “Hey”
Spot in
Memory
0 1 2 3 4 5 6 7
Stored
Value
78 “Hey”
Reserved For
Array
Reserved For
Array
Reserved For
Array
Spot in
Memory
0 1 2 3 4 5 6 7
Memory
int String int[] int[] int[]
Type
Stored
Value
78 “Hey” 1 2 3
Memory
Spot in
Memory
0 1 2 3 4 5 6 7
Memory
int String int[] int[] int[]
Type
Stored
Value
78 “Hey” 1 2 3
And so on, deal wit any element index -- so the size of array can't be changed
10/19/22 Dr/ Ayman Soliman 114
q The Array - Arrays as a Data Structure
O(1)
O(1) O(n)
O(1) O(n)
Index 0 1 2 3 4
Numbers
1 2 3 4
Insert 1 at Index 0
Index 0 1 2 3 4
Numbers
1 2 3 4
Insert 1 at Index 0
Index 0 1 2 3 4
Numbers
1 2 3 4
Insert 1 at Index 0
Index 0 1 2 3 4
Numbers
1 1 2 3 4
Index 0 1 2 3 4
Numbers
1 2 3 4 5
Index 0 1 2 3 4
Numbers
1 2 3 4 5
Numbers[0] = Numbers[1]
Index 0 1 2 3 4
Numbers
2 2 3 4 5
Numbers[1] = Numbers[2]
Index 0 1 2 3 4
Numbers
2 3 3 4 5
Numbers[2] = Numbers[3]
Index 0 1 2 3 4
Numbers
2 3 3 4 5
Numbers[3] = Numbers[4]
Index 0 1 2 3 4
Numbers
2 3 3 5 5
Numbers[4] = null;
Index 0 1 2 3 4
Numbers
2 3 3 5
Pros Cons
Very basic. Easy to learn and master Can be wasting storage space
Overall, pretty reliable. Has some flaws as well as advantages. Can be used in almost any program if need
be, but sometimes you may want extra functionality