0% found this document useful (0 votes)
21 views

Java9e PPT ch09

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Java9e PPT ch09

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Java Programming, 9e

Chapter 9

Advanced Array Concepts

1
Objectives

• Sort array elements using the bubble sort algorithm


• Sort array elements using the insertion sort algorithm
• Use two-dimensional and other multidimensional arrays
• Use the Arrays class
• Use the ArrayList class
• Create enumerations

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

• Jagged array (ragged array)


• A two-dimensional array with rows of different lengths
• To create a jagged array:
• Define the number of rows for a two-dimensional array
• Do not define the number of columns in the rows
• Then declare the individual rows

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)

Table 9-3 Useful methods of


the ArrayList class
Method Purpose
public void add(Object) Adds an item to an ArrayList; the default version adds an
public void add(int, Object) item at the next available location; an overloaded version
allows you to specify a position at which to add the item
public void remove(int) Removes an item from an ArrayList at a specified location
public void set(int, Object) Alters an item at a specified ArrayList location
Object get(int) Retrieves an item from a specified location in an ArrayList
Public int size() Returns the current ArrayList size

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)

• Enumerated data type


• A programmer-created data type with a fixed set of values
• To create an enumerated data type, use:
• The keyword enum
• An identifier for the type
• A pair of curly braces that contain a list of the enum constants
enum Month {JAN, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC};

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)

Table 9-4 Some useful


nonstatic enum methods
Method Description Example if mon = Month.MAY

toString() Returns the name of the calling mon.toString() is "MAY"


constant object
ordinal() Returns an integer that represents mon.ordinal() is 4
the constant’s position in the list of
constants; as with arrays, the first
position is 0
equals() Returns true if its argument is mon.equals(Month.MAY) is true
equal to the calling object’s value mon.equals(Month.NOV) is false
compareTo() Returns a negative integer if the mon.compareTo(Month.JUL) is negative
calling object’s ordinal value is less mon.compareTo(Month.FEB) is positive
than that of the argument, 0 if they mon.compareTo(Month.MAY) is 0
are the same, and a positive
integer if the calling object’s
ordinal value is greater than that of
the argument

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)

Table 9-5 Some static


enum methods
Method Description Example with Month
Enumeration
valueOf() Accepts a string parameter Month.valueOf("DEC") returns
and returns an enumeration the DEC enum constant
constant
values() Returns an array of the Month.values() returns an
enumerated constants array with 12 elements that
contain the enum constants

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)

• You can declare an enumerated type in its own file


• Filename matches the type name and has a .java extension
• You can use comparison operators with enumeration constants
• You can use enumerations to control a switch structure

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)

• Advantages of creating an enumeration type:


• Only allowed values can be assigned
• Using enums makes the values type-safe
• Provides a form of self-documentation
• You can also add methods and other fields to an enum type
• Type-safe
• Describes a data type for which only appropriate behaviors are allowed

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.

You might also like