Slides 06
Slides 06
by Ali Kassem
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
Chapter 6 focuses on:
Ali Kassem 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
Ali Kassem 3
Arrays
A particular value in an array is referenced using the array
name followed by the index in brackets
scores[2]
Ali Kassem 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;
Ali Kassem 5
Arrays
The values held in an array are called array elements
Ali Kassem 7
Declaring Arrays
Some examples of array declarations:
boolean[] flags;
Ali Kassem 8
Bounds Checking
Once an array is created, it has a fixed size
Ali Kassem 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]);
Ali Kassem 10
Bounds Checking
Each array object has a public constant called length that
stores the size of the array
It is referenced using the array name:
scores.length
Note that length holds the number of elements, not the
largest index
See ReverseOrder.java (page 324)
Ali Kassem 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[];
Ali Kassem 12
Initializer Lists
An initializer list can be used to instantiate and initialize an
array in one step
The values are delimited by braces and separated by
commas
Examples:
Ali Kassem 13
Initializer Lists
Note that when an initializer list is used:
• the new operator is not used
Ali Kassem 14
Arrays as Parameters
An entire array can be passed as a parameter to a method
Ali Kassem 15
Arrays of Objects
The elements of an array can be object references
Ali Kassem 16
Command-Line Arguments
The signature of the main method indicates that it takes an
array of String objects as a parameter
These values come from command-line arguments that are
provided when the interpreter is invoked
For example, the following invocation of the interpreter
passes an array of three String objects into main:
> java StateEval pennsylvania texas arizona
Ali Kassem 17
Arrays of Objects
Objects can have arrays as instance variables
Ali Kassem 18
Sorting
Sorting is the process of arranging a list of items in a
particular order
The sorting process is based on specific value(s)
• Selection Sort
• Insertion Sort
Ali Kassem 19
Selection Sort
The approach of Selection Sort:
• select a value and put it in its final place into the list
• repeat for all other values
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
Ali Kassem 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
Ali Kassem 21
Swapping
Swapping is the process of exchanging two values
temp = first;
first = second;
second = temp;
Ali Kassem 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
Ali Kassem 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
Ali Kassem 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
See SortPhoneList.java (page 347)
See Contact.java (page 348)
See Sorts.java (page 343) – the second
insertionSort method
Ali Kassem 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
Approximately n2 number of comparisons are made to sort
a list of size n
We therefore say that these sorts are of order n2
Ali Kassem 26
Two-Dimensional Arrays
A one-dimensional array stores a list of elements
one two
dimension dimensions
Ali Kassem 27
Two-Dimensional Arrays
To be precise, a two-dimensional array in Java is an array
of arrays
A two-dimensional array is declared by specifying the size
of each dimension separately:
int[][] scores = new int[12][50];
Ali Kassem 29
Multidimensional Arrays
An array can have many dimensions
Ali Kassem 30
The ArrayList Class
The ArrayList class is part of the java.util package
Ali Kassem 31
ArrayList Efficiency
The ArrayList class is implemented using an array
Ali Kassem 32
Polygons and Polylines
Arrays often are helpful in graphics processing
Ali Kassem 33
The Rocket Program
Ali Kassem 34
The Polygon Class
The Polygon class, defined in the java.awt package can
be used to define and draw a polygon
Ali Kassem 35
Check Boxes
A check box is a button that can be toggled on or off
Ali Kassem 36
The StyleOptions Program
A frame is a container that can be used to create stand-
alone GUI applications
Ali Kassem 37
The StyleOptions Program
Ali Kassem 38
Radio Buttons
A set of radio buttons represents a set of mutually exclusive
options
Ali Kassem 39
The QuoteOptions Program
Ali Kassem 40
Summary
Chapter 6 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
• polygons and polylines
• more button components
Ali Kassem 41