CPCS204 02 Arrays
CPCS204 02 Arrays
Review of Arrays
int examGrades[30];
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 3
Types of Data Structures
Array
Linked List
Queue Tree
Stack
These are just a few of the data structures
you will study during your career here at KAU.
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 4
Importance of Data Structures
Goal:
We need to organize data
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 6
Operations Performed on Data
Structures
Deletion
Removing a data element from the structure
Sorting
Arrange the data elements in some logical fashion
ascending or descending
Merging
Combining data elements form two or more data
structures into one
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 7
Types of Data Structures
Based on Memory Allocation:
Static (or fixed sized) data structures
Such as arrays
Dynamic data structures (change size as needed)
Such as Linked Lists
(coming later in the semester)
Based on Representation
Linear representation
Such as arrays and linked lists
Non-linear representation
Such as trees and graphs
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 8
Brief Interlude: Human Stupidity
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 9
Motivation for Data Structures
All data structures have a PURPOSE
A reason for their creation and motivation
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 10
Array: Motivation
You want to store 5 numbers in a program
No problem. You define five int variables:
int num1, num2, num3, num4, num5;
Easy enough, right?
But what if you want to store 1000 numbers?
Are you really going to make 1000 separate variables?
int num1, num2,..., num998, num999, num1000;
That would be CRAZY!
So what is the solution?
A data structure! Specifically, an array!
An array is one of the most common data structures.
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 11
What is an Array?
An array is a data structure
It is a collection of homogeneous data elements
Meaning, all elements are of the same type
Examples:
An array of student grades
An array of student names
An array of objects (OOP perspective!)
Array elements (or their references) are stored in
contiguous/consecutive memory locations
Also, an array is a static data structure
An array cannot grow or shrink during program
execution…the size is fixed
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 12
Review of Arrays
Basic Concepts
Array name (data)
Index/subscript (0...9)
The slots are numbered sequentially
starting at zero (Java, C++)
If there are N slots in an array, the index
will be 0 through N-1
Array length = N = 10
Array size = N x Size of an element = 40
Direct access to an element
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 13
Review of Arrays
Homogeneity:
All elements in an array must have the same data
type
Contiguous memory:
Array elements are stored in contiguous memory
locations
So an array of 100 integers (int is 4 bytes) would
be stored in 400 bytes of contiguous memory
Each of the 4 byte locations would come right after the
previous location in memory
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 14
Review of Arrays
Using Arrays:
Array_name[index]
For example, in Java
System.out.println(data[4]);
will display 0
data[3] = 99;
Will replace -3 with 99
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 15
Review of Arrays
More Concepts:
data[ -1 ] always illegal
data[ 10 ] illegal (10 > upper bound)
data[ 1.5 ] always illegal
data[ 0 ] always OK
data[ 9 ] OK
5 10 18 30 45 50 60 65 70 80
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 17
Review of Arrays
Array Dimensionality
Two dimensional (matrix or table)
Example: 2x4 matrix (2 rows, 4 columns)
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 18
Review of Arrays
2D Arrays:
Given the following array (whose name is ‘M’)
20 25 60 40
30 15 70 90
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 21
Review of Arrays
Array Declaration:
You declare an array as follows:
int[] grades;
This simply makes a an array variable (grades)
But it does NOT specify where that variable refers to
We can also declare the following:
int[] grades = new int[10];
Now the array variable grades refers to an array of ten
integers
By default, numeric elements are initialized to zero
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 22
Review of Arrays
More info on Arrays:
When the array is created, memory is reserved for
its contents
You can also specify the values for the array
instead of using the new operator
Example:
int[] grade = {95, 93, 88}; //array of 3 ints
To find the length of an array, use the length
method
int numGrades = grade.length();
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 23
Review of Arrays
Arrays’ Properties in Java
Arrays are objects
Arrays are created dynamically, at run time
An array type variable holds a reference to the
array object
An array’s length is set when the array is created,
and it cannot be changed.
Arrays can be duplicated with the
Object.clone() method
An ArrayIndexOutOfBoundsException is
thrown if index exceeds array length
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 24
Review of Arrays
Array Processing in JAVA
java.util.Arrays class provides many built-in
functions for sorting and searching
The method Arrays.sort(array_name) is used to sort an
array
The method Arrays.binarySearch(array_name, target) is
used to search through a sorted array
The method Arrays.equals(array1, array2) is used to
check if two arrays are equal
Meaning, if the same values are at each index position
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 25
Array Processing in Java
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 26
Review of Arrays
WASN’T
THAT
INCREDIBLE!
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 27
Daily Demotivator
© Dr. Jonathan (Yahya) Cazalas Data Structure Intro & Review of Arrays page 28
Data Structure Intro &
Review of Arrays