Chapter 2 - Arrays
Chapter 2 - Arrays
Chapter 2 - Arrays
The Array
Most commonly used data structure
Common operations
Insertion
Searching
Deletion
How do these differ for an ‘ordered array’?
How do these differ for an array which does not allow
duplicates?
1
10/11/2018
Array Storage
An array is a collection of data of the same type
Stored linearly in memory:
Collections
Collection: is a structured data type, that stores
data and provide operations for adding, removing,
updating data in the collection.
2
10/11/2018
3
10/11/2018
Sequential Access
4
10/11/2018
10
5
10/11/2018
11
The Size
Size of an array cannot change once it’s been declared:
intArray = new int[100];
But, one nice thing is that arrays are objects. So you can
access its size easily:
int arrayLength = intArray.length;
12
6
10/11/2018
Access
Done by using an index number in square brackets:
int temp = intArray[3]; // Gets 4th element
intArray[7] = 66; // Sets 8th element
13
Initialization
What do the elements of this array contain:
int[] intArray = new int[100];
How about this one:
BankAccount[] myAccounts = new BankAccount[100];
What happens if we attempt to access one of these values?
14
7
10/11/2018
Ask ourselves:
How could we make the initialization shorter?
How could we save declaring nElems?
15
16
8
10/11/2018
Abstraction
This illustrates the concept of abstraction
The way in which an operation is performed inside a class is
invisible
Client of HighArray performs more complex operations
through simple method invocations
Never directly accesses the private data in the array
17