Java9e PPT ch09
Java9e PPT ch09
Chapter 9
1
Objectives
2
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Sorting Array Elements Using the Bubble
Sort Algorithm (1 of 2)
• Sorting
• The process of arranging a series of objects in some logical order
• Ascending order
• Begin with the object that has the lowest value
• Descending order
• Begin with the object that has the largest value
3
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Sorting Array Elements Using the Bubble
Sort Algorithm (2 of 2)
• Simplest possible sort
• Involves two values that are out of order
• Swap two values (assume valA = 16 and valB = 2)
temp = valA; // 16 goes to temp
valA = valB; // 2 goes to valA
valB = temp; // 16 goes to valB
4
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Using the Bubble Sort Algorithm (1 of 3)
• Bubble sort
• You continue to compare pairs of items, swapping them if they are out of order
• The smallest items “bubble” to the top of the list, eventually creating a sorted list
5
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Using the Bubble Sort Algorithm (2 of 3)
6
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Using the Bubble Sort Algorithm (3 of 3)
7
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Sorting Arrays of Objects
• You can sort arrays of objects in much the same way that you sort arrays of
primitive types
• Major difference
- Make the comparison that determines whether to swap two array elements
- Sort based on a particular object field
8
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Sorting Array Elements Using the Insertion
Sort Algorithm (1 of 2)
• Insertion sort
• A sorting algorithm that enables you to look at each list element one at a time
• Move items down if the tested element should be inserted prior to other elements
• Similar to the technique that sorts a group of objects manually
9
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Sorting Array Elements Using the Insertion
Sort Algorithm (2 of 2)
10
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Using Two-Dimensional and Other
Multidimensional Arrays (1 of 3)
• One-dimensional or single-dimensional array
• An array that you can picture as a column of values
• Elements are accessed using a single subscript
• Two-dimensional arrays
• Have two or more columns of values
• Have rows and columns
• Use two subscripts
• Are often called a matrix or table
int[][] someNumbers = new int[3][4];
11
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Using Two-Dimensional and Other
Multidimensional Arrays (2 of 3)
12
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Using Two-Dimensional and Other
Multidimensional Arrays (3 of 3)
int[][] rents = { {400, 450, 510},
{500, 560, 630},
{625, 676, 740},
{1000, 1250, 1600} };
13
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Passing a Two-Dimensional Array to a
Method
• Pass the array name just as you do with a one-dimensional array
public static void displayScores(int[][]scoresArray)
14
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Using the length Field with a Two-
Dimensional Array
• The length field holds the number of rows in the array
rents.length
• Each row has a length field that holds the number of columns in the row
rents[1].length
15
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Understanding Jagged Arrays
16
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Using Other Multidimensional Arrays
• Multidimensional arrays
• Arrays with more than one dimension
• Create arrays of any size
• Keep track of the order of variables needed as subscripts
• Do not exhaust your computer’s memory
17
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Using the Arrays Class (1 of 4)
• Arrays class
• Contains many useful methods for manipulating arrays
• static methods
- Use them with the class name without instantiating an Arrays object
• binarySearch() method
- A convenient way to search through sorted lists of values of various data types
- The list must be in order
18
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Using the Arrays Class (2 of 4)
Table 9-2 Useful methods of
the Arrays class
Method Purpose
static int binarySearch(type[] a, Searches the specified array for the specified key
type key) value using the binary search algorithm
static boolean equals(type[] a, Returns true if the two specified arrays of the same
type[] a2) type are equal to one another
static void fill(type[] a, type val) Assigns the specified value to each element of the
specified array
static void sort(type[] a) Sorts the specified array into ascending order
static void sort(type[] a, Sorts the specified range of the array into
int fromIndex, int toIndex) ascending order
static void parallelSort(type[] a) Sorts the specified array into ascending order
static void parallelSort(type[] a, Sorts the specified range of the array into
int fromIndex, int toIndex) ascending order
19
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Using the Arrays Class (3 of 4)
20
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Using the Arrays Class (4 of 4)
21
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Using the ArrayList Class (1 of 3)
• The ArrayList class provides some advantages over the Arrays class
• Dynamically resizable
• Can add an item at any point in an ArrayList container
• Can remove an item at any point in an ArrayList container
• Capacity
• The number of items an ArrayList can hold without having to increase its size
22
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Using the ArrayList Class (2 of 3)
23
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Using the ArrayList Class (3 of 3)
24
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Creating Enumerations (1 of 7)
25
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Creating Enumerations (2 of 7)
26
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Creating Enumerations (3 of 7)
27
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Creating Enumerations (4 of 7)
28
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Creating Enumerations (5 of 7)
29
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Creating Enumerations (6 of 7)
30
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Creating Enumerations (7 of 7)
31
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Don’t Do It
• Don’t forget that the first subscript used with a two-dimensional array
represents the row, and that the second subscript represents the column
• Don’t try to store primitive data types in an ArrayList structure
• Don’t think enum constants are strings; they are not enclosed in quotes
32
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Summary
• Sorting
• The process of arranging a series of objects in some logical order
• Bubble sort and Insertion sort
• Two-dimensional arrays
• Both rows and columns
• Arrays class
• ArrayList class
• A programmer-created data type with a fixed set of values is an enumerated
data type
33
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.