0% found this document useful (0 votes)
2 views33 pages

Lecture 02 - Arrays (Autosaved)

The document outlines a lecture on Arrays as part of the Higher National Diploma in Information Technology program at SLIIT Academy. It covers key learning objectives, definitions, implementations, and algorithms related to arrays, including their advantages and disadvantages. The document also discusses methods for creating, initializing, and traversing arrays, as well as operations like insertion, deletion, and searching.
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)
2 views33 pages

Lecture 02 - Arrays (Autosaved)

The document outlines a lecture on Arrays as part of the Higher National Diploma in Information Technology program at SLIIT Academy. It covers key learning objectives, definitions, implementations, and algorithms related to arrays, including their advantages and disadvantages. The document also discusses methods for creating, initializing, and traversing arrays, as well as operations like insertion, deletion, and searching.
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/ 33

SLIIT Academy

Higher National Diploma in Information Technology– Year 2,


Semester 2

Data Structures and Algorithms

Arrays
Gayana Fernando

SLIIT Academy Pvt Ltd. © 2022


Lecture 02 - Arrays
By Dr Gayana Fernando
Leaning Objectives
LO1: Critically analyze data structures for a given problem and use the most suitable data structure
that can be use when implementing a solution for a given problem.

LO2 : Design algorithms for solving problems that use data structure Array

LO4: Develop the ability to compare and contrast the performance of data structures.

SLIIT Academy Pvt Ltd. © 2022


Outline

• Introduction
• Array
Implementation
• Array Algorithms

SLIIT Academy Pvt Ltd. © 2022


What we learned
last week
• What are data
structures ?
• What are algorithms ?
• What are the usages of
data structures ?
• What do you mean by
efficiency of a program
?
What is an Array?
• An array is a group of variables of the same data type and referred to
by a common name.
• An array is a block of consecutive memory locations that hold values
of the same data type.

Value Value Value Value Value Value


…….
01 02 03 04 05 06

• Most used data structure.


SLIIT Academy Pvt Ltd. © 2022 6
• You might come across a situation where you
need to store set of similar type of values.
• To store the marks of students.
Why Do We • To store prices of items.
• To store details of election candidates.
Need Arrays? • To store the tax rates of employees.
• These details can be stored one after the other
(as a sequence).

SLIIT Academy Pvt Ltd. © 2022 7


More about Arrays
• Individual locations are called array’s elements.
• Each location is identified by a number, called index or subscript.
• An index can have any integer value from 0 to array’s length - 1.
Indexes

0 1 2 3 4 5 6
Value Value Value Value Value Value Value
01 02 03 04 05 06 07

Elements
SLIIT Academy Pvt Ltd. © 2022 8
Array Index

• Specific array elements are


referred to by using array’s
name and the element’s index.
• Index is written within square
brackets following array’s name)
• Array Indices start from 0.
• Therefore the 1st element of an
array a is referred to as
arrayName[0] and the nth
element as arrayName[n-1].
SLIIT Academy Pvt Ltd. © 2022 9
Array Length
• In Java, the length of an array is fixed at the
time of its creation.
• Java interpreter checks the values of indices
at run time and throws
ArrayIndexOutOfBoundsException if an
index is negative or if it is greater than the
length of the array - 1.
• The biggest disadvantage of arrays

SLIIT Academy Pvt Ltd. © 2022 10


Arrays as Objects

• In Java, an array is an object.


• An array can hold elements of any data type; primitive or any class
type.
• If the type of its elements are anyType, then the type of the array is
anyType[ ].
• Recall: String args[]

SLIIT Academy Pvt Ltd. © 2022 11


How to Create an Array?

• Array declaration:
anyType [] arrayName;
• Like other objects, the declaration creates only a reference, initially set to null.
An array must be created before it can be used.
• One way to create an array:
arrayName = new anyType [length];

SLIIT Academy Pvt Ltd. © 2022 12


What really happens…
• When an array is created, space is allocated to hold its elements and the
elements get the default values.
anyType [] arrName = new anyType [length];

arrName d.v.
d.v.
d.v.
d.v

SLIIT Academy Pvt Ltd. © 2022 13


Some Examples
int [] taxRate= new int[10]; length 10, all
elements are set to 0
String [] words = new String[100];
Length 100 , all
double [] gasPrices; elements are set to
null
gasPrices = new double[12];

Rectangle [] recList = new Rectangle[5];


length 5, all elements
are set to null

SLIIT Academy Pvt Ltd. © 2022 14


Create an Array: Method 2
• The other way to create an array is:
anyType [] arrName = {values separated by commas};
• Here array is declared and initialized in one statement.
• Examples:

String [] candidates = {“Ranil",“Sajith“,”anura”}


double [] gasPrices = {370,380,400};

SLIIT Academy Pvt Ltd. © 2022 15


How to get the Array Length?
• The length of an array is determined when that array is created.
• The length is either given explicitly or comes from the length of the
{…} initialization list.
• The length of an array arrayName is referred to in the code as
arrayName.length
• length is a public property (not a method) in an array object.

SLIIT Academy Pvt Ltd. © 2022 16


Initialization of Array Elements
• Unless specific values are given in a {…} list, all the elements are
initialized to the default value:
0 for numbers,
false for booleans
null for objects or Strings.

SLIIT Academy Pvt Ltd. © 2022 17


Traverse through an Array
• An array can be traversed through using any loop: (for/while/do-
while)

double [] gasPrices = {3.05,3.17,3.59};

for(int c=0; c<gasPrices.length; c++)


{
...

SLIIT Academy Pvt Ltd. © 2022 18


Unordered Arrays

Image credit : https://www.deviantart.com/orderoftheroyalwolf/art/Creepypasta-Height-Chart-563950697


Unordered Arrays
1. Create a class called “ArrayEx” with following properties.
• An array to integer numbers
• A variable to hold the maximum size
• A variable to hold the number of values in the array
2. Write a constructure to initiate properties
3. Write a method to insert a value to the array.
4. Write a method to search a value in the array. The method should return array index if you
find it, if not -1.
5. Write a method to delete a value in the array. If the value is there , we need to move all the
items with higher index values down one elements to fill in the ‘hole’ left by the deleted
element.
6. Write a method to display all the values in the array.
01 : Class Properties

numberList maxSize

count
02: Constructer
0
myArray maxSize
1 5
2

3
0 index
4
03: Insert a Value
0
myArray maxSize
1 5
2

3
0 index
4
60,50,10
03: Delete a Value
0 60
myArray maxSize
1 50
5
2 10
3
3 index
4
60,50,10
Ordered Arrays

Image credit : https://www.wattpad.com/802382478-poems-and-random-stuff-height


Ordered Arrays
• The values are arranged according to an order (Ascending /
Descending)

0 1 2 3

10 20 30 40
Ordered Arrays
• Modify the insert method.
• Need to find the correct location (above the smaller value and lower the
larger value).
• Move all the larger values to get space.
03: Insert a Value
0 60
myArray maxSize
1 5
2

3
0 index
4
60,50,10,40
public void addValue(int value)
{
int i;
for(i = 0;i < count; i++)
{
if(numberList[i] > value) {
break;
}
}
for(int j = count; j > i; j--)
{
numberList[j]= numberList[j-1];
}
nunberList[i] = value;
count++;
}
Ordered Arrays - Searching a
value
• We can use “Binary Search” algorithm to look for a value in
an ordered array.
• Searching is efficient in ordered arrays.
Arrays

Advantages Disadvantages
• Accessing /Searching a value is • Fixed Size
easy • Inserting / deleting to ordered
• Implementation is simple array are not efficient.
Array – efficiency
• If the array contains N elements
• Inserting a new element will take O(n) time
• Searching / Deleting
• Worst case – O(n)
• Best case – O(1)

You might also like