Transcript Document

Chapter 8:
Part 3
Collections and
Two-dimensional arrays
Objectives
• One-Dimensional Arrays
• Array Initialization
• The Arrays Class: Searching and Sorting
• Arrays as Arguments
• The Collections Framework: ArrayLists
• Two-Dimensional Arrays
• Common Programming Errors
2
3
The Collections Framework:
ArrayLists
• Array
– Data structure of choice for fixed-length collections of data
that are related
• Many programming applications require variablelength lists
– Java provides a set of classes referred to as the collections
framework
– Provides seven different types of generic data structures
4
5
The Collections Framework
• The Collections class:
– Supports container classes
– Provides functions for:
• Searching
• Sorting
• Random shuffling
• Reverse-ordering
6
7
8
9
The Iterator Class
• Similar to an array’s index
• Generalized index that keeps track of object’s
position within a container
• For some classes it provides the primary means of
accessing individual elements
• Obtaining an iterator:
– Iterator iter = x.iterator();
10
Parallel Arrays as Records
• Parallel arrays:
– Corresponding data in a record resides in the same
position in more than one array
– Required in earlier programming languages that only
supported array data structures
– Can combine parallel elements in an object
• Store objects in a one-dimensional array
11
Two-Dimensional Arrays
• Consist of both rows and columns of elements
• Sometimes called tables
• Example declaration:
– int val[][];
• Example allocation:
– val = new int[3][4];
• Elements are identified by position in an array
12
Two-Dimensional Arrays
(continued)
• Can be initialized from within declaration
statements:
– int val[][] = {{8,16,9,52}, {3,15,27,6}, {7,25,2,10}};
• Number of columns need not be the same for each
row
• May be displayed by:
– Individual element notation
– Using loops
• Usually nested loops
13
14
Two-Dimensional Array
Length
• val.length
– Provides the number of rows in the array referenced by
val
• val[i].length
– Provides the number of columns in the ith row of val
array
15
Passing Two-Dimensional
Arrays
• Identical to passing a one-dimensional array
– The called method receives access to the entire array
16
Advanced Dimensioning
Capabilities
• Can create two-dimensional arrays where each
row has a different number of columns
• To create:
– Initialize a list that explicitly lists values for each row
– Or use the new operator and place values into the newly
created array
17
Larger Dimensional Arrays
• Can have any number of array dimensions
• Similar to creating and allocating two-dimensional
arrays
• Declaration:
int val[][][];
val = new int[3][2][2];
18
19
Common Programming Errors
• Forgetting the empty bracket pairs when declaring
an array’s name
• Declaring an array reference variable using
explicit dimension sizes
• Using a subscript that references a nonexistent
array element
• Not using a large enough counter value in a for
loop counter to cycle through all array elements
20
Summary
• One-dimensional array:
– Data structure
– Stores list of values of same data type
• Array elements:
– Stored in contiguous locations in memory
– Referenced using the array name and a subscript
• Such as num[22]
21
Summary (continued)
• Two-dimensional array is declared by providing:
– Data type
– Reference variable name
– Two sets of empty bracket pairs after the array’s name
• Arrays may be initialized when they are declared
• Collections framework
– Set of classes providing generic data structures
22