Slides Array
Slides Array
Presentation slides are copyright 2002 by John Lewis and William Loftus. All rights reserved.
Instructors using the textbook may use and modify these slides for pedagogical purposes.
Arrays
Arrays are objects that help us organize large amounts of
information
2
Arrays
An array is an ordered list of values
0 1 2 3 4 5 6 7 8 9
scores 79 87 94 82 67 98 87 81 74 91
3
Arrays
A particular value in an array is referenced using the array
name followed by the index in brackets
scores[2]
4
Arrays
For example, an array element can be assigned a value,
printed, or used in a calculation:
scores[2] = 89;
scores[first] = scores[first] + 2;
5
Arrays
The values held in an array are called array elements
Note that the type of the array does not specify its size, but
each object of that type has a specific size
7
Declaring Arrays
Some examples of array declarations:
boolean[] flags;
8
Bounds Checking
Once an array is created, it has a fixed size
9
Bounds Checking
For example, if the array codes can hold 100 values, it can
be indexed using only the numbers 0 to 99
System.out.println (codes[count]);
10
Bounds Checking
Each array object has a public constant called length that
stores the size of the array
scores.length
11
Alternate Array Syntax
The brackets of the array type can be associated with the
element type or with the name of the array
float[] prices;
float prices[];
12
Initializer Lists
An initializer list can be used to instantiate and initialize an
array in one step
Examples:
int[] units = {147, 323, 89, 933, 540,
269, 97, 114, 298, 476};
13
Initializer Lists
Note that when an initializer list is used:
• the new operator is not used
14
Arrays as Parameters
An entire array can be passed as a parameter to a method
15
Arrays of Objects
The elements of an array can be object references
16
Command-Line Arguments
The signature of the main method indicates that it takes an
array of String objects as a parameter
17
Arrays of Objects
Objects can have arrays as instance variables
18
Sorting
Sorting is the process of arranging a list of items in a
particular order
In more detail:
• find the smallest value in the list
• switch it with the value in the first position
• find the next smallest value in the list
• switch it with the value in the second position
• repeat until all values are in their proper places
20
Selection Sort
An example:
original: 3 9 6 1 2
smallest is 1: 1 9 6 3 2
smallest is 2: 1 2 6 3 9
smallest is 3: 1 2 3 6 9
smallest is 6: 1 2 3 6 9
21
Swapping
Swapping is the process of exchanging two values
22
Insertion Sort
The approach of Insertion Sort:
• pick any item and insert it into its proper place in a sorted sublist
• repeat until all items have been inserted
In more detail:
• consider the first item to be a sorted sublist (of one item)
• insert the second item into the sorted sublist, shifting the first item
as needed to make room to insert the new addition
• insert the third item into the sorted sublist (of two items), shifting
items as necessary
• repeat until all values are inserted into their proper positions
23
Insertion Sort
An example:
original: 3 9 6 1 2
insert 9: 3 9 6 1 2
insert 6: 3 6 9 1 2
insert 1: 1 3 6 9 2
insert 2: 1 2 3 6 9
24
Sorting Objects
Integers have an inherent order, but the ordering criteria of
a collection of objects must be defined
Recall that a Java interface can be used as a type name and
guarantees that a particular class implements particular
methods
We can use the Comparable interface and the
compareTo method to develop a generic sort for a set of
objects
25
Comparing Sorts
Both Selection and Insertion sorts are similar in efficiency
They both have outer loops that scan all elements, and
inner loops that compare the value of the outer loop with
almost all values in the list
26
Two-Dimensional Arrays
A one-dimensional array stores a list of elements
one two
dimension dimensions
27
Two-Dimensional Arrays
To be precise, a two-dimensional array in Java is an array
of arrays
29
Multidimensional Arrays
An array can have many dimensions
If it has more than one dimension, it is called a
multidimensional array
Each dimension subdivides the previous one into the
specified number of elements
Each array dimension has its own length constant
Because each dimension is an array of array references, the
arrays within one dimension can be of different lengths
• these are sometimes called ragged arrays
30
The ArrayList Class
The ArrayList class is part of the java.util package
Like an array, it can store a list of values and reference
them with an index
Unlike an array, an ArrayList object grows and shrinks
as needed
Items can be inserted or removed with a single method
invocation
It stores references to the Object class, which allows it to
store any kind of object
31
ArrayList Efficiency
The ArrayList class is implemented using an array
32
Summary
This Chapter has focused on:
• array declaration and use
• passing arrays and array elements as parameters
• arrays of objects
• sorting elements in an array
• multidimensional arrays
• the ArrayList class
33