Java Programming Fifth Edition: Arrays
Java Programming Fifth Edition: Arrays
Arrays
Objectives
Declare and initialize an array Use subscripts with an array Declare an array of objects Search an array for an exact match Search an array for a range match
Objectives (continued)
Pass arrays to and return arrays from methods Manipulate arrays of Strings Sort array elements Use two-dimensional and multidimensional arrays Use the Arrays class Use the ArrayList class
Subscript
Integer contained within square brackets Indicates one of arrays variables or elements
Initializing an Array
Variable with reference type
Such as array Holds memory address where value stored
Array names
Represent computer memory addresses Contain references
10
Power of arrays
Use subscripts that are variables Rather than constant subscripts Use a loop to perform array operations for (sub = 0; sub < 5; ++sub)
scoreArray[sub] += 3;
11
12
length field
Contains number of elements in array for(sub = 0; sub < scoreArray.length; ++sub)
scoreArray[sub] += 3;
13
14
15
16
17
18
19
20
Range match
Compare value to endpoints of numerical ranges Find category in which value belongs
21
22
Passed by value
Copy of value made and used in receiving method All primitive types passed this way
23
24
25
26
27
28
Ascending order
Begin with object that has lowest value
Descending order
Start with object that has largest value
29
30
31
32
33
34
Two-dimensional arrays
Two or more columns of values Rows and columns Use two subscripts Matrix or table int[][] someNumbers = new int[3][4];
35
36
37
Each row has a length field that holds the number of columns in the row
rents[1].length
38
39
40
binarySearch() method
Convenient way to search through sorted lists of values of various data types List must be in order
Java Programming, Fifth Edition 41
42
Capacity
Number of items ArrayList can hold without having to increase its size
43
44
45
You Do It
Creating and populating an array Initializing an array Using a for loop to access array elements Creating parallel arrays to eliminate nested if statements Creating an application with an array of objects
46
You Do It (continued)
Creating an interactive application that creates an array of objects Passing an array to a method Using Arrays class methods
47
Dont Do It
Dont forget that the lowest array subscript is 0 Dont forget that the highest array subscript is one less than the length Dont forget that length is an array property and not a method Dont place a subscript after an objects field or method name when accessing an array of objects Dont forget that array names are references
48
Dont Do It (continued)
Dont use brackets with an array name when you pass it to a method Dont assume that an array of characters is a string Dont forget that the first subscript used with a twodimensional array represents the row, and that the second subscript represents the column
49
Summary
Array
Named list of data items All have same type
Array names
Represent computer memory addresses
length field
Contains number of elements in array
Java Programming, Fifth Edition 50
Summary (continued)
Search array to find match to value Perform range match Pass single array element to method Sorting
Process of arranging series of objects in some logical order
Two-dimensional arrays
Both rows and columns