0% found this document useful (0 votes)
35 views143 pages

INF210 Lec3 Data Structures

Uploaded by

Nader Yasser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views143 pages

INF210 Lec3 Data Structures

Uploaded by

Nader Yasser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 143

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

10/19/22 Dr/ Ayman Soliman 2


Arrays

One -Dimensional Two -Dimensional

10/19/22 Dr/ Ayman Soliman 3


One –Dimensional Array

10/19/22 Dr/ Ayman Soliman 4


1) Introduction
Ø The array is a common data structure taught in most programming classes...

§ But today we’ll be covering the Array more in-depth as a data structure

Time Complexity Storage Methods

10/19/22 Dr/ Ayman Soliman 5


q The Array - Array Basics
Ø An array is fundamentally a list of similar values

Ø Can be used to store anything


§ Usernames
§ High Scores
§ Prices
Ø Store values of the same type
§ Integer
§ String
§ Float

10/19/22 Dr/ Ayman Soliman 6


q The Array - Array Basics
Ø An array is fundamentally a list of similar values

Ø Can be used to store anything


§ Usernames
§ High Scores
§ Prices
Ø Store values of the same type
§ Integer
§ String
§ Float

10/19/22 Dr/ Ayman Soliman 7


q The Array - Array Basics
Ø An array is fundamentally a list of similar values

Ø Can be used to store anything


§ Usernames
§ High Scores
§ Prices
Ø Store values of the same type
§ Integer
§ String
§ Float

10/19/22 Dr/ Ayman Soliman 8


q The Array - Array Basics
Ø An array is fundamentally a list of similar values

Ø Can be used to store anything


§ Usernames
§ High Scores
§ Prices
Ø Store values of the same type
§ Integer
§ String
§ Float

10/19/22 Dr/ Ayman Soliman 9


q The Array - Array Basics
Ø An array is fundamentally a list of similar values

Ø Can be used to store anything , 7,8,9,


, 4, 5, 6
0,1,2,3 1...
§ Usernames 10,1
§ High Scores
§ Prices
Ø Store values of the same type
§ Integer
§ String
§ Float

10/19/22 Dr/ Ayman Soliman 10


q The Array - Array Basics
Ø An array is fundamentally a list of similar values
“H
Ø Can be used to store anything ell
oW
or
§ Usernames ld”
§ High Scores
§ Prices
Ø Store values of the same type
§ Integer
§ String
§ Float

10/19/22 Dr/ Ayman Soliman 11


q The Array - Array Basics
Ø An array is fundamentally a list of similar values
3.141
Ø Can be used to store anything 46264 592653589
33832 79323
71693 9502884 8
7
§ Usernames 99375 19
10
§ High Scores
§ Prices
Ø Store values of the same type
§ Integer
§ String
§ Float

10/19/22 Dr/ Ayman Soliman 12


q The Array - Array Basics
Ø Every item in the list of data is referred to as an “element”

Ø The collective total of elements is the array

Elements

1 2 3 4 5 6 7 8 9 10

10/19/22 Dr/ Ayman Soliman 13


q The Array - Array Basics
Ø Every item in the list of data is referred to as an “element”

Ø The collective total of elements is the array

Array

1 2 3 4 5 6 7 8 9 10

10/19/22 Dr/ Ayman Soliman 14


q The Array - Array Basics
Ø Every item in the list of data is referred to as an “element”

Ø The collective total of elements is the array

This is going to be true for almost all Data Structures we


talk about,so keep this in mind

10/19/22 Dr/ Ayman Soliman 15


q The Array - Array Basics
Ø An array usually has 3 attributes

§ A Name

§ A Type

§ A Size

Name Type Size

10/19/22 Dr/ Ayman Soliman 16


q The Array - Array Names
Ø A name is simply a name for the array

§ Used to reference and interact with it

Names “John Smith” “Gary Vee” “David Lee” “Adam Knox”

Salaries 10,000 12,500 8,750 15,000

10/19/22 Dr/ Ayman Soliman 17


q The Array - Array Names
Ø A name is simply a name for the array

§ Used to reference and interact with it

Names “John Smith” “Gary Vee” “David Lee” “Adam Knox”

Salaries 10,000 12,500 8,750 15,000

Search through “Salaries” Array

10/19/22 Dr/ Ayman Soliman 18


q The Array - Array Names
Ø A name is simply a name for the array

§ Used to reference and interact with it

Names “John Smith” “Gary Vee” “David Lee” “Adam Knox”

Salaries 10,000 12,500 8,750 15,000

Search through “Names” Array

10/19/22 Dr/ Ayman Soliman 19


q The Array - Parallel Arrays
Ø These are also examples of parallel arrays

§ 2 or more arrays which…

o Contain the same number of elements

o Have corresponding values in the same position

Names “John Smith” “Gary Vee” “David Lee” “Adam Knox”

Salaries 10,000 12,500 8,750 15,000

10/19/22 Dr/ Ayman Soliman 20


q The Array - Parallel Arrays
Ø These are also examples of parallel arrays

§ 2 or more arrays which…

o Contain the same number of elements

o Have corresponding values in the same position

Names “John Smith” “Gary Vee” “David Lee” “Adam Knox”

Salaries 10,000 12,500 8,750 15,000

10/19/22 Dr/ Ayman Soliman 21


q The Array - Parallel Arrays
Ø Parallel arrays are extremely useful for storing differing types of data about the
same entity

Names “John Smith” “Gary Vee” “David Lee” “Adam Knox”

Salaries 10,000 12,500 8,750 15,000

10/19/22 Dr/ Ayman Soliman 22


q The Array - Array Types
Ø An array’s type

§ what type of information is stored or will be stored within that array

§ MUST hold all the same type of information

Wrong “Hello” 5 2 “World”

10/19/22 Dr/ Ayman Soliman 23


q The Array - Array Types
Ø An array’s type

§ what type of information is stored or will be stored within that array

§ MUST hold all the same type of information

Wrong “Hello” 5 2 “World”

Right 5 2

Right “Hello” “World”

10/19/22 Dr/ Ayman Soliman 24


q The Array - Array Size
Ø An array’s size is a set integer that is fixed upon creation of the array

§ Represents the total amount of elements that can be stored within the array

§ Cannot be changed

34 ...
35 Define an array

The array’s size cannot be changed by conventional methods


past this point

10/19/22 Dr/ Ayman Soliman 25


q The Array - Array Basics
Ø An array usually has 3 attributes

§ A Name

§ A Type

§ A Size

Name Type Size

10/19/22 Dr/ Ayman Soliman 26


q The Array - Array Basics
Ø An array usually has 3 attributes

§ A Name

§ A Type

§ A Size

Name Type Size

10/19/22 Dr/ Ayman Soliman 27


q The Array - Array Basics
Ø An array usually has 3 attributes

§ A Name

§ A Type

§ A Size

Name Type Size

10/19/22 Dr/ Ayman Soliman 28


q The Array - Creating Arrays
Ø There are 2 different ways to create an array in most languages

§ Populate the array with elements right then and there

§ Set a specific size for the array, then populate it later

Populate First Populate Later

Grocery List Eggs Milk Meat

10/19/22 Dr/ Ayman Soliman 29


q The Array - Creating Arrays
Ø There are 2 different ways to create an array in most languages

§ Populate the array with elements right then and there

§ Set a specific size for the array, then populate it later

Populate First Populate Later

Grocery List Eggs Milk Meat Grocery List

10/19/22 Dr/ Ayman Soliman 30


q The Array - Creating Arrays
Ø There are 2 different ways to create an array in most languages

§ Populate the array with elements right then and there

§ Set a specific size for the array, then populate it later

Populate First Populate Later

Grocery List Eggs Milk Meat Grocery List Eggs

10/19/22 Dr/ Ayman Soliman 31


q The Array - Creating Arrays
Ø There are 2 different ways to create an array in most languages

§ Populate the array with elements right then and there

§ Set a specific size for the array, then populate it later

Populate First Populate Later

Grocery List Eggs Milk Meat Grocery List Eggs Milk

10/19/22 Dr/ Ayman Soliman 32


q The Array - Creating Arrays
Ø There are 2 different ways to create an array in most languages

§ Populate the array with elements right then and there

§ Set a specific size for the array, then populate it later

Populate First Populate Later

Grocery List Eggs Milk Meat Grocery List Eggs Milk Meat

10/19/22 Dr/ Ayman Soliman 33


q The Array - Creating Arrays
Ø There are 2 different ways to create an array in most languages

§ Populate the array with elements right then and there

§ Set a specific size for the array, then populate it later

Populate First Populate Later

Grocery List Eggs Milk Meat Grocery List Eggs Milk Meat

10/19/22 Dr/ Ayman Soliman 34


q The Array - Populate-First Arrays
Ø Defining and filling an array as soon as you create it is used mainly for when
you already know which values are going to be held within it

Salaries 1 2 3 4 5 6 7 8 9 10

Read from a text file

10/19/22 Dr/ Ayman Soliman 35


q The Array - Populate-First Arrays
Ø Defining and filling an array as soon as you create it is used mainly for when
you already know which values are going to be held within it

Salaries 10K 15K 13K 11K 25K 13K 13K 20K 15K 8K

Read from a text file

10/19/22 Dr/ Ayman Soliman 36


q The Array - Populate-First Arrays
Ø The way you do this varies from language to language

§ Shown in Java, Python, and C#

int array[] = { 1, 2, 3 };

array = [1, 2, 3]

int[] array = { 1, 2, 3 };

10/19/22 Dr/ Ayman Soliman 37


q The Array - Populate-First Arrays
Ø The way you do this varies from language to language

§ Shown in Java, Python, and C#

int array[] = { 1, 2, 3 };

array = [1, 2, 3]

int[] array = { 1, 2, 3 };

10/19/22 Dr/ Ayman Soliman 38


q The Array - Populate-First Arrays
Ø The way you do this varies from language to language

§ Shown in Java, Python, and C#

int array[] = { 1, 2, 3 };

array = [1, 2, 3]

int[] array = { 1, 2, 3 };

10/19/22 Dr/ Ayman Soliman 39


q The Array - Populate-First Arrays
Ø The way you do this varies from language to language

§ Shown in Java, Python, and C#

int array[] = { 1, 2, 3 };

array = [1, 2, 3]

int[] array = { 1, 2, 3 };

10/19/22 Dr/ Ayman Soliman 40


q The Array - Populate-First Arrays
Ø The way you do this varies from language to language

§ Shown in Java, Python, and C#

int array[] = { 1, 2, 3 };

array = [1, 2, 3]

int[] array = { 1, 2, 3 };

10/19/22 Dr/ Ayman Soliman 41


q The Array - Populate-First Arrays
Ø The way you do this varies from language to language

§ Shown in Java, Python, and C#

int array[] = { 1, 2, 3 };

array = [1, 2, 3]

int[] array = { 1, 2, 3 };

10/19/22 Dr/ Ayman Soliman 42


q The Array - Populate-First Arrays
Ø The way you do this varies from language to language

§ Shown in Java, Python, and C#

int array[] = { 1, 2, 3 };

array = [1, 2, 3]

int[] array = { 1, 2, 3 };

10/19/22 Dr/ Ayman Soliman 43


q The Array - Populate-First Arrays
Ø The way you do this varies from language to language

§ Shown in Java, Python, and C#

int array[] = { 1, 2, 3 };

array = [1, 2, 3]

int[] array = { 1, 2, 3 };

10/19/22 Dr/ Ayman Soliman 44


q The Array - Populate-First Arrays
Ø The way you do this varies from language to language

§ Shown in Java, Python, and C#

int array[] = { 1, 2, 3 };

array = [1, 2, 3]

int[] array = { 1, 2, 3 };

10/19/22 Dr/ Ayman Soliman 45


q The Array - Populate-First Arrays
Ø The way you do this varies from language to language

§ Shown in Java, Python, and C#

int array[] = { 1, 2, 3 };

array = [1, 2, 3]

int[] array = { 1, 2, 3 };

10/19/22 Dr/ Ayman Soliman 46


q The Array - Populate-First Arrays
Ø The way you do this varies from language to language

§ Shown in Java, Python, and C#

int array[] = { 1, 2, 3 };

array = [1, 2, 3]

int[] array = { 1, 2, 3 };

10/19/22 Dr/ Ayman Soliman 47


q The Array - Populate-First Arrays
Ø The way you do this varies from language to language

§ Shown in Java, Python, and C#

int array[] = { 1, 2, 3 };

array = [1, 2, 3]

int[] array = { 1, 2, 3 };

10/19/22 Dr/ Ayman Soliman 48


q The Array - Populate-First Arrays
Ø The way you do this varies from language to language

§ Shown in Java, Python, and C#

Size = 3

int array[] = { 1, 2, 3 };

array = [1, 2, 3]

int[] array = { 1, 2, 3 };

10/19/22 Dr/ Ayman Soliman 49


q The Array - Populate-First Arrays
Ø The way you do this varies from language to language

§ Shown in Java, Python, and C#

Size = 4

int array[] = { 1, 2, 3, 4 };

array = [1, 2, 3, 4]

int[] array = { 1, 2, 3, 4 };

10/19/22 Dr/ Ayman Soliman 50


q The Array - Populate-Later Arrays
Ø Creating an array by setting an initial size for our array, but not filling it with
any elements

§ Slowly populate it as the programs run

§ Used for user-entered information

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

10/19/22 Dr/ Ayman Soliman 51


q The Array - Populate-Later Arrays
Ø The way you do this varies from language to language

§ Shown in Java and C#

int array[] = new int[10];

int[] array = new int[10];

10/19/22 Dr/ Ayman Soliman 52


q The Array - Populate-Later Arrays
Ø The way you do this varies from language to language

§ Shown in Java and C#

int array[] = new int[10];

int[] array = new int[10];

10/19/22 Dr/ Ayman Soliman 53


q The Array - Populate-Later Arrays
Ø The way you do this varies from language to language

§ Shown in Java and C#

int array[] = new int[10];

int[] array = new int[10];

10/19/22 Dr/ Ayman Soliman 54


q The Array - Populate-Later Arrays
Ø The way you do this varies from language to language

§ Shown in Java and C#

int array[] = new int[10];

int[] array = new int[10];

10/19/22 Dr/ Ayman Soliman 55


q The Array - Populate-Later Arrays
Ø The way you do this varies from language to language

§ Shown in Java and C#

int array[] = new int[10];

int[] array = new int[10];

10/19/22 Dr/ Ayman Soliman 56


q The Array - Populate-Later Arrays
Ø The way you do this varies from language to language

§ Shown in Java and C#

int array[] = new int[10];


Size = FINAL

int[] array = new int[10];

10/19/22 Dr/ Ayman Soliman 57


q The Array - Populate-Later Arrays
Ø The way you do this varies from language to language

§ Shown in Java and C#

int array[] = new int[20];


Size = FINAL

int[] array = new int[20];

10/19/22 Dr/ Ayman Soliman 58


q The Array - Populate-Later Arrays
Ø The way you do this varies from language to language

§ Shown in Java and C#

int array[] = new int[10];

int[] array = new int[10];

10/19/22 Dr/ Ayman Soliman 59


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

Index

Numbers
1 2 3 4 5 6 7 8 9 10

10/19/22 Dr/ Ayman Soliman 60


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

Index 0

Numbers
1 2 3 4 5 6 7 8 9 10

10/19/22 Dr/ Ayman Soliman 61


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

Index 0 1

Numbers
1 2 3 4 5 6 7 8 9 10

10/19/22 Dr/ Ayman Soliman 62


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

Index 0 1 2 3 4 5 6 7 8 9

Numbers
1 2 3 4 5 6 7 8 9 10

10/19/22 Dr/ Ayman Soliman 63


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

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

10/19/22 Dr/ Ayman Soliman 64


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

Index 0 1 2 3 4 5 6 7 8 9

Numbers
1 2 3 4 5 6 7 8 9 10

10/19/22 Dr/ Ayman Soliman 65


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

Index 0 1 2 3 4 5 6 7 8 9

Numbers
1 2 3 4 5 6 7 8 9 10

10/19/22 Dr/ Ayman Soliman 66


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

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

10/19/22 Dr/ Ayman Soliman 67


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

Index 0 1 2 3 4 5 6 7 8 9

Numbers
1 2 3 4 5 6 7 8 9 10

10/19/22 Dr/ Ayman Soliman 68


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

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/19/22 Dr/ Ayman Soliman 69


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

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])

10/19/22 Dr/ Ayman Soliman 70


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

Index 0 1 2 3 4 5 6 7 8 9

Numbers
1 2 3 4 5 6 7 8 9 10

10/19/22 Dr/ Ayman Soliman 71


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

Index 0 1 2 3 4 5 6 7 8 9

Numbers
1 2 3 4 5 6 7 8 9 10

The last element of array has 9th index number

10/19/22 Dr/ Ayman Soliman 72


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

Index 0 1 2 3 4 5 6 7 8 9

Numbers
1 2 3 4 5 6 7 8 9 10

10/19/22 Dr/ Ayman Soliman 73


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

Index 0 1 2 3 4 5 6 7 8 9

Numbers
1 2 3 4 5 6 7 8 9 10

10/19/22 Dr/ Ayman Soliman 74


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

Index 0 1 2 3 4 5 6 7 8 9

Numbers
1 2 3 4 5 6 7 8 9 10

10/19/22 Dr/ Ayman Soliman 75


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

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])

10/19/22 Dr/ Ayman Soliman 76


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

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])

10/19/22 Dr/ Ayman Soliman 77


q The Array - Numerical Indexes
Ø To get information that is stored within the array, we use a numerical index

§ An integer which corresponds to an element within the array

Index 0 1 2 3 4 5 6 7 8 9

Numbers
1 2 3 4 5 6 7 8 9 10

Out of bounds error

10/19/22 Dr/ Ayman Soliman 78


q The Array - Replacing information in an Array
Ø Referencing an arrays index is also how we replace elements within an array

Index 0 1 2 3 4 5 6 7 8 9

Numbers
1 2 3 4 5 6 7 8 9 10

10/19/22 Dr/ Ayman Soliman 79


q The Array - Replacing information in an Array
Ø Referencing an arrays index is also how we replace elements within an array

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 change the last element from 10 to 11

10/19/22 Dr/ Ayman Soliman 80


q The Array - Replacing information in an Array
Ø Referencing an arrays index is also how we replace elements within an array

Index 0 1 2 3 4 5 6 7 8 9

Numbers
1 2 3 4 5 6 7 8 9 10

10/19/22 Dr/ Ayman Soliman 81


q The Array - Replacing information in an Array
Ø Referencing an arrays index is also how we replace elements within an array

Index 0 1 2 3 4 5 6 7 8 9

Numbers
1 2 3 4 5 6 7 8 9 10

Numbers[9]

10/19/22 Dr/ Ayman Soliman 82


q The Array - Replacing information in an Array
Ø Referencing an arrays index is also how we replace elements within an array

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

10/19/22 Dr/ Ayman Soliman 83


q The Array - Replacing information in an Array
Ø Referencing an arrays index is also how we replace elements within an array

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

10/19/22 Dr/ Ayman Soliman 84


q The Array - Replacing information in an Array
Ø Referencing an arrays index is also how we replace elements within an array

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

10/19/22 Dr/ Ayman Soliman 85


q The Array - Replacing information in an Array
Ø Referencing an arrays index is also how we replace elements within an array

Index 0 1 2 3 4 5 6 7 8 9

Numbers
1 2 3 4 5 6 7 8 9 10

10/19/22 Dr/ Ayman Soliman 86


Two –Dimensional Array

10/19/22 Dr/ Ayman Soliman 87


Ø Contents
1) 2-Dimensional Arrays

2) Arrays as a Data Structure – Accessing

3) Arrays as a Data Structure - Searching

4) Arrays as a Data Structure - Inserting

5) Arrays as a Data Structure - Deleting

6) Pros and Cons

10/19/22 Dr/ Ayman Soliman 88


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 4
Array

Ø An array with an array at each element is known as a two-dimensional array.

Ø Think of two-dimensional array as a matrix.

Ø Its like any other array.

10/19/22 Dr/ Ayman Soliman 89


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 4

10/19/22 Dr/ Ayman Soliman 90


q The Array - 2-Dimensional Arrays
Ø An array with an array at each index is known as a 2-dimensional array

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

Chess board Bingo board


10/19/22 Dr/ Ayman Soliman 92
q The Array - 2-Dimensional Arrays
Ø An array with an array at each index is known as a 2-dimensional array

Ø Two-dimensional array is useful during programming as following:

Chess board Bingo board image


10/19/22 Dr/ Ayman Soliman 93
q The Array - 2-Dimensional Arrays
Ø Referencing an element within a 2-dimensional array is mostly the same as
with 1-dimensional arrays, except you now need 2 indexes

§ One for the column and row

Index 0 1 2 3

10/19/22 Dr/ Ayman Soliman 94


q The Array - 2-Dimensional Arrays
Ø Referencing an element within a 2-dimensional array is mostly the same as
with 1-dimensional arrays, except you now need 2 indexes

§ One for the column and row

Index 0 1 2 3

0 “Steven” “Alex” “Dave” “Jake”

1 “Adam” “Lucas” “Quinten” “John”

2 “Sean” “Marcus” “Carl” “Jackson”

3 “Peter” “Cam” “Anthony” “Ethan”


If we search on “Carl”, we must determine the specified row and column
10/19/22 Dr/ Ayman Soliman 95
q The Array - 2-Dimensional Arrays
Ø Referencing an element within a 2-dimensional array is mostly the same as
with 1-dimensional arrays, except you now need 2 indexes

§ One for the column and row

Index 0 1 2 3

0 “Steven” “Alex” “Dave” “Jake”

1 “Adam” “Lucas” “Quinten” “John”

2 “Sean” “Marcus” “Carl” “Jackson”

3 “Peter” “Cam” “Anthony” “Ethan”


If we search on “Carl”, we must determine the specified row and column
10/19/22 Dr/ Ayman Soliman 96
q The Array - 2-Dimensional Arrays
Ø Referencing an element within a 2-dimensional array is mostly the same as
with 1-dimensional arrays, except you now need 2 indexes

§ One for the column and row

Index 0 1 2 3

0 “Steven” “Alex” “Dave” “Jake”

1 “Adam” “Lucas” “Quinten” “John”

2 “Sean” “Marcus” “Carl” “Jackson”

3 “Peter” “Cam” “Anthony” “Ethan”


If we search on “Carl”, we must determine the specified row and column
10/19/22 Dr/ Ayman Soliman 97
q The Array - 2-Dimensional Arrays
Ø Referencing an element within a 2-dimensional array is mostly the same as
with 1-dimensional arrays, except you now need 2 indexes

§ One for the column and row

Index 0 1 2 3

0 “Steven” “Alex” “Dave” “Jake”

1 “Adam” “Lucas” “Quinten” “John”

22 “Sean” “Marcus” “Carl” “Jackson”

3 “Peter” “Cam” “Anthony” “Ethan”


If we search on “Carl”, we must determine the specified row and column
10/19/22 Dr/ Ayman Soliman 98
q The Array - 2-Dimensional Arrays
Ø Referencing an element within a 2-dimensional array is mostly the same as
with 1-dimensional arrays, except you now need 2 indexes

§ One for the column and row

Index 0 1 2 3

0 “Steven” “Alex” “Dave” “Jake”

1 “Adam” “Lucas” “Quinten” “John”

22 “Sean” “Marcus” “Carl”


“Carl” “Jackson”

3 “Peter” “Cam” “Anthony” “Ethan”


If we search on “Carl”, we must determine the specified row and column
10/19/22 Dr/ Ayman Soliman 99
q The Array - 2-Dimensional Arrays
Ø Referencing an element within a 2-dimensional array is mostly the same as
with 1-dimensional arrays, except you now need 2 indexes

§ One for the column and row

Index 0 1 2 3

0 “Steven” “Alex” “Dave” “Jake”

1 “Adam” “Lucas” “Quinten” “John”

2 “Sean” “Marcus” “Carl” “Jackson”

3 “Peter” “Cam” “Anthony” “Ethan”


If we search on “Adam”, we must determine the specified row and column
10/19/22 Dr/ Ayman Soliman 100
q The Array - 2-Dimensional Arrays
Ø Referencing an element within a 2-dimensional array is mostly the same as
with 1-dimensional arrays, except you now need 2 indexes

§ One for the column and row

Index 0 1 2 3

0 “Steven” “Alex” “Dave” “Jake”

1 “Adam” “Lucas” “Quinten” “John”

2 “Sean” “Marcus” “Carl” “Jackson”

3 “Peter” “Cam” “Anthony” “Ethan”


If we search on “Adam”, we must determine the specified row and column
10/19/22 Dr/ Ayman Soliman 101
q The Array - 2-Dimensional Arrays
Ø Referencing an element within a 2-dimensional array is mostly the same as
with 1-dimensional arrays, except you now need 2 indexes

§ One for the column and row

Index 0 1 2 3

0 “Steven” “Alex” “Dave” “Jake”

1 “Adam” “Lucas” “Quinten” “John”

2 “Sean” “Marcus” “Carl” “Jackson”

3 “Peter” “Cam” “Anthony” “Ethan”


If we search on “Adam”, we must determine the specified row and column
10/19/22 Dr/ Ayman Soliman 102
q The Array - 2-Dimensional Arrays
Ø Referencing an element within a 2-dimensional array is mostly the same as
with 1-dimensional arrays, except you now need 2 indexes

§ One for the column and row

Index 0 1 2 3

0 “Steven” “Alex” “Dave” “Jake”

1 “Adam” “Lucas” “Quinten” “John”

2 “Sean” “Marcus” “Carl” “Jackson”

3 “Peter” “Cam” “Anthony” “Ethan”


If we search on “Adam”, we must determine the specified row and column
10/19/22 Dr/ Ayman Soliman 103
q The Array --- n-dimensional arrays

3-dimensional array 4-dimensional array


10/19/22 Dr/ Ayman Soliman 104
q The Array - Arrays as a Data Structure

Accessing Searching Inserting Deleting

10/19/22 Dr/ Ayman Soliman 105


q The Array - Arrays as a Data Structure

Accessing Searching Inserting Deleting

10/19/22 Dr/ Ayman Soliman 106


q The Array - Arrays as a Data Structure

Accessing Searching Inserting Deleting

O(1) Means element accessing can be conducted in an instantaneous of one time

10/19/22 Dr/ Ayman Soliman 107


q The Array - Arrays as a Data Structure

Memory

Spot in
Memory
0 1 2 3 4 5 6 7

Memory
int String
Type

Stored
Value
78 “Hey”

10/19/22 Dr/ Ayman Soliman 108


q The Array - Arrays as a Data Structure
Let's look at a fake portion of memory in the computer
Memory You can see we have some information near the beginning, maybe
some integers or strings that we have initialized, that’s not important

Spot in
Memory
0 1 2 3 4 5 6 7

Memory
int String
Type

Stored
Value
78 “Hey”

10/19/22 Dr/ Ayman Soliman 109


q The Array - Arrays as a Data Structure
The important point is that when we get to the part in our code
Memory Which instantiates an array, we already know exactly how large
the array is going to be

Spot in
Memory
0 1 2 3 4 5 6 7

Memory
int String
Type

Stored
Value
78 “Hey”

10/19/22 Dr/ Ayman Soliman 110


q The Array - Arrays as a Data Structure

Memory int array[] = new int[3];

Spot in
Memory
0 1 2 3 4 5 6 7

Memory
int String
Type

Stored
Value
78 “Hey”

Determine the correct index of array to deal with it


10/19/22 Dr/ Ayman Soliman 111
q The Array - Arrays as a Data Structure

Memory int array[] = new int[3];

Spot in
Memory
0 1 2 3 4 5 6 7

Memory Reserved For Reserved For Reserved For


int String Array Array Array
Type

Stored
Value
78 “Hey”
Reserved For
Array
Reserved For
Array
Reserved For
Array

10/19/22 Dr/ Ayman Soliman 112


q The Array - Arrays as a Data Structure

Memory int array[] = new int[3];

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

10/19/22 Dr/ Ayman Soliman 113


q The Array - Arrays as a Data Structure

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

Accessing Searching Inserting Deleting

O(1)

10/19/22 Dr/ Ayman Soliman 115


q The Array - Arrays as a Data Structure

Accessing Searching Inserting Deleting

O(1) O(n)

10/19/22 Dr/ Ayman Soliman 116


q The Array - Arrays as a Data Structure
Ø Searching through arrays is O(n) because most of the time we are working
with unsorted lists

Ø must use linear search

Names “Dave” “Bob” “Adam” “Frank” “Evan” “Gary”

10/19/22 Dr/ Ayman Soliman 117


q The Array - Arrays as a Data Structure
Ø Searching through arrays is O(n) because most of the time we are working
with unsorted lists

Ø must use linear search

Names “Dave” “Bob” “Adam” “Frank” “Evan” “Gary”

10/19/22 Dr/ Ayman Soliman 118


q The Array - Arrays as a Data Structure
Ø Searching through arrays is O(n) because most of the time we are working
with unsorted lists

Ø must use linear search

Names “Dave” “Bob” “Adam” “Frank” “Evan” “Gary”

10/19/22 Dr/ Ayman Soliman 119


q The Array - Arrays as a Data Structure
Ø Searching through arrays is O(n) because most of the time we are working
with unsorted lists

Ø must use linear search

Names “Dave” “Bob” “Adam” “Frank” “Evan” “Gary”

10/19/22 Dr/ Ayman Soliman 120


q The Array - Arrays as a Data Structure
Ø Searching through arrays is O(n) because most of the time we are working
with unsorted lists

Ø must use linear search

Names “Dave” “Bob” “Adam” “Frank” “Evan” “Gary”

10/19/22 Dr/ Ayman Soliman 121


q The Array - Arrays as a Data Structure
Ø Searching through arrays is O(n) because most of the time we are working
with unsorted lists

Ø must use linear search

Names “Dave” “Bob” “Adam” “Frank” “Evan” “Gary”

10/19/22 Dr/ Ayman Soliman 122


q The Array - Arrays as a Data Structure
Ø Searching through arrays is O(n) because most of the time we are working
with unsorted lists

Ø must use linear search

Names “Dave” “Bob” “Adam” “Frank” “Evan” “Gary”

10/19/22 Dr/ Ayman Soliman 123


q The Array - Arrays as a Data Structure
Ø Searching through arrays is O(n) because most of the time we are working
with unsorted lists

Ø must use linear search

Names “Dave” “Bob” “Adam” “Frank” “Evan” “Gary”

10/19/22 Dr/ Ayman Soliman 124


q The Array - Arrays as a Data Structure
Ø Searching through arrays is O(n) because most of the time we are working
with unsorted lists

Ø must use linear search

Names “Dave” “Bob” “Adam” “Frank” “Evan” “Gary”

You may search on “Dave”, this is the lowest execution time

Search For: “Dave”

10/19/22 Dr/ Ayman Soliman 125


q The Array - Arrays as a Data Structure
Ø Searching through arrays is O(n) because most of the time we are working
with unsorted lists

Ø must use linear search

Names “Dave” “Bob” “Adam” “Frank” “Evan” “Gary”

Ø When using BigO notation, we always use worst-case scenario

Search For: “Gary”

10/19/22 Dr/ Ayman Soliman 126


q The Array - Arrays as a Data Structure

Accessing Searching Inserting Deleting

O(1) O(n)

10/19/22 Dr/ Ayman Soliman 127


q The Array - Arrays as a Data Structure

Accessing Searching Inserting Deleting

O(1) O(n) O(n)

10/19/22 Dr/ Ayman Soliman 128


q The Array - Arrays as a Data Structure
Ø Inserting is O(n) because inserting an element within the array requires you to
shift every element that’s after the index you want to insert the value at to the
right one space

Index 0 1 2 3 4

Numbers
1 2 3 4

10/19/22 Dr/ Ayman Soliman 129


q The Array - Arrays as a Data Structure
Ø Inserting is O(n) because inserting an element within the array requires you to
shift every element that’s after the index you want to insert the value at to the
right one space

Insert 1 at Index 0

Index 0 1 2 3 4

Numbers
1 2 3 4

10/19/22 Dr/ Ayman Soliman 130


q The Array - Arrays as a Data Structure
Ø Inserting is O(n) because inserting an element within the array requires you to
shift every element that’s after the index you want to insert the value at to the
right one space

Insert 1 at Index 0

Index 0 1 2 3 4

Numbers
1 2 3 4

10/19/22 Dr/ Ayman Soliman 131


q The Array - Arrays as a Data Structure
Ø Inserting is O(n) because inserting an element within the array requires you to
shift every element that’s after the index you want to insert the value at to the
right one space

Insert 1 at Index 0

Index 0 1 2 3 4

Numbers
1 1 2 3 4

10/19/22 Dr/ Ayman Soliman 132


q The Array - Arrays as a Data Structure

Accessing Searching Inserting Deleting

O(1) O(n) O(n)

10/19/22 Dr/ Ayman Soliman 133


q The Array - Arrays as a Data Structure

Accessing Searching Inserting Deleting

O(1) O(n) O(n) O(n)

10/19/22 Dr/ Ayman Soliman 134


q The Array - Arrays as a Data Structure
Ø Deleting is O(n) because deleting an element within the array requires you to
shift every element to the right of the one you want to delete down one index

Index 0 1 2 3 4

Numbers
1 2 3 4 5

10/19/22 Dr/ Ayman Soliman 135


q The Array - Arrays as a Data Structure
Ø Deleting is O(n) because deleting an element within the array requires you to
shift every element to the right of the one you want to delete down one index

Delete number Index [0]

Index 0 1 2 3 4

Numbers
1 2 3 4 5

10/19/22 Dr/ Ayman Soliman 136


q The Array - Arrays as a Data Structure
Ø Deleting is O(n) because deleting an element within the array requires you to
shift every element to the right of the one you want to delete down one index

Numbers[0] = Numbers[1]

Index 0 1 2 3 4

Numbers
2 2 3 4 5

10/19/22 Dr/ Ayman Soliman 137


q The Array - Arrays as a Data Structure
Ø Deleting is O(n) because deleting an element within the array requires you to
shift every element to the right of the one you want to delete down one index

Numbers[1] = Numbers[2]

Index 0 1 2 3 4

Numbers
2 3 3 4 5

10/19/22 Dr/ Ayman Soliman 138


q The Array - Arrays as a Data Structure
Ø Deleting is O(n) because deleting an element within the array requires you to
shift every element to the right of the one you want to delete down one index

Numbers[2] = Numbers[3]

Index 0 1 2 3 4

Numbers
2 3 3 4 5

10/19/22 Dr/ Ayman Soliman 139


q The Array - Arrays as a Data Structure
Ø Deleting is O(n) because deleting an element within the array requires you to
shift every element to the right of the one you want to delete down one index

Numbers[3] = Numbers[4]

Index 0 1 2 3 4

Numbers
2 3 3 5 5

10/19/22 Dr/ Ayman Soliman 140


q The Array - Arrays as a Data Structure
Ø Deleting is O(n) because deleting an element within the array requires you to
shift every element to the right of the one you want to delete down one index

Numbers[4] = null;

Index 0 1 2 3 4

Numbers
2 3 3 5

10/19/22 Dr/ Ayman Soliman 141


q The Array - Pros and Cons

Pros Cons

Size of the array cannot be changed once


Good for storing similar contiguous data
initialized

O(1) Accessing Power Inserting and Deleting are not efficient

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

10/19/22 Dr/ Ayman Soliman 142


10/19/22 Dr/ Ayman Soliman 143

You might also like