HO 03 Array
HO 03 Array
Array: motivation
You want to store 5 numbers in a computer
Define 5 variables, e.g. num1, num2, ..., num5
What, if you want to store 1000 numbers?
Defining 1000 variables is not feasible!
Requires much programming effort
Any better solution?
Yes, some structured data type
o Array is one of the most common structured data types
o Saves a lot of programming effort (cf. 1000 variable
names)
What is an Array?
A collection of data elements in which
all elements are of the same data type, hence
homogeneous data
o An array of students’ marks
o An array of students’ names
o An array of objects (OOP perspective!)
elements (or their references) are stored at
contiguous/ consecutive memory locations
Array is a static data structure
An array cannot grow or shrink during program
execution – its size is fixed
The array data structure
An array is an indexed sequence of components
Typically, the array occupies sequential storage locations
The length of the array is determined when the array is
created, and cannot be changed
Each component of the array has a fixed, unique index
Indices range from a lower bound to an upper bound
4
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
Array variations I
The array indices may be integers – Java
The lower bound is zero – Java
In most languages, arrays are homogeneous (all
components must have the same type); in some (Lisp,
Prolog) the components may be heterogeneous (of
varying types)
6
Homogeneity
All elements in the array must have the same
data type
Index: 0 1 2 3 4 5 6 7 8 9
Value: 5 10 18 30 45 50 60 65 70 80
Index: 0 1 2 3 4
Value: 5.5 10.2 18.5 45.6 60.5
Index: 0 1 2 3 4
Value: ‘A’ 10.2 55 ‘X’ 60.5 Not an array
Array variations II
In an object-oriented language, arrays may be objects
(Java) or not objects (C++)
8
Arrays in Java I
9
Arrays in Java II
Arrays are objects
Arrays are allocated by new, manipulated by reference, and
garbage collected
However, the usual bracket notation a[i] is provided as
syntactic sugar
Arrays are reflective
a.length is the length of array a
a.getClass() is the type of array a
An array of integers has type [I
10
Contiguous Memory
Array elements are stored at contiguous memory
locations
Index: 0 1 2 3 4 5 6 7 8 9
Value: 5 10 18 30 45 50 60 65 70 80
class tag [I
length 4
0 17
1 23
2 948
3 3
12
Using Arrays
Array_name[index]
For example, in Java
System.out.println(data[4]) will display
0
0 1 2 3 4 5 6 7 8 9
array a [I 10 1 1 2 3 5 8 13 21 34 55
subarray a[2...6]
15
Array as an ADT
An array is an Abstract Data Type
The array type has a set of values
The values are all the possible arrays
The array type has a set of operations that can be
applied uniformly to each of these values
The only operation is indexing
Alternatively, this can be viewed as two operations:
inspecting an indexed location
16
Subarray as an ADT
As noted earlier, to use a subarray, you must keep track of (1) the
array itself, (2) the lower bound, and (3) the upper bound
This suggests:
class Subarray<V> {
V[ ] subarray;
int lowerBound;
int upperBound;
// Constructor, some methods...
}
Advantage:
Only one object to pass around
Disadvantages:
The subarray must hold Objects, not primitives
You lose the nice array syntax
17
Array’s Dimensionality
One dimensional (just a linear list)
e.g., 5 10 18 30 45 50 60 65 70 80
Only one subscript is required to access an individual
element
21
2D arrays in Java
Java doesn’t have “real” 2D arrays, but array elements can
themselves be arrays:
int x[][] denotes an array x of array components, each of which is
an array of integer components
We can define the above array like this:
x = new int[5][8];
and treat it as a regular 2D array
This is an array of 5 arrays
Each subarray is an array of 8 ints
22
Ragged arrays
23
Array Operations
Indexing: inspect or update an element using its index.
Performance is very fast O(1)
randomNumber = numbers[5];
numbers[20000] = 100;
Insertion: add an element at certain index
– Start: very slow O(n) because of shift
– End : very fast O(1) because no need to shift
Removal: remove an element at certain index
– Start: very slow O(n) because of shift
– End : very fast O(1) because no need to shift
Search: performance depends on algorithm
1) Linear: slow O(n) 2) binary : O(log n)
Sort: performance depends on algorithm
1) Bubble: slow O(n2) 2) Selection: slow O(n2)
3) Insertion: slow O(n2) 4)Merge : O (n log n)
Inserting an element into an array
Suppose we want to insert the value 8 into this sorted array
(while keeping the array sorted)
1 3 3 7 12 14 17 19 22 30
We can do this by shifting all the elements after the mark right by
one location
Of course, we have to discard the 30 when we do this
1 3 3 7 8 12 14 17 19 22
• Moving all those elements makes this a slow
operation (linear in the size of the array)
25
Deleting an element from an array
Deleting an element is similar--again, we have to move all
the elements after it
1 3 3 7 8 12 14 17 19 22
1 3 3 7 12 14 17 19 22 ?
26
Arrays and ArrayLists
Here are the advantages of arrays:
Very efficient, in terms of both time and space
Convenient syntax
Here is the main disadvantage:
Fixed size
27
Vectors and ArrayLists
Vector methods include the following:
public void insertElementAt(Object elem, int index)
public void removeElementAt(int index)
ArrayList methods include the following:
public void add(int index, Object elem)
public void remove(int index)
These are slow (linear time) methods
28
Conclusions
Arrays are not identical in all languages
29
The End
30