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

Java PPT Ch08

Uploaded by

Hol Gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Java PPT Ch08

Uploaded by

Hol Gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

Java Programming

Sixth Edition
Chapter 8: Arrays
Objectives
• Declare and initialize an array
• Use subscripts with an array
• Declare and use arrays of objects
• Search an array
• Pass arrays to and return arrays from methods

Java Programming, Sixth Edition 2


Declaring and Initializing an Array
• Array
– Named list of data items
– All have same type
• Declare array variable
– Same way as declaring any simple variable
– Insert pair of square brackets after type

Java Programming, Sixth Edition 3


Declaring and Initializing an Array
(cont'd.)
• double[] salesFigure;
• int[] idNum;
• Still need to reserve memory space
– sale = new double[20];
– double[] sale = new double[20];
• Subscript
– Integer contained within square brackets
– Indicates one of array’s variables or elements

Java Programming, Sixth Edition 4


Declaring and Initializing an Array
(cont'd.)
• Array’s elements numbered beginning with zero
– Can legally use any subscript from 0 through 19
– When working with array that has 20 elements
• Work with any individual array element
– Treat no differently than single variable of same type
– Example: sale[0] = 2100.00;

Java Programming, Sixth Edition 5


Declaring and Initializing an Array
(cont'd.)

Java Programming, Sixth Edition 6


Initializing an Array
• Variable with reference type
– Such as array
– Holds memory address where value stored
• Array names
– Represent computer memory addresses
– Contain references
• Declare array name
– No computer memory address assigned
– Has special value null
• Unicode value ‘\u0000’
Java Programming, Sixth Edition 7
Initializing an Array (cont'd.)
• Use keyword new to define array
– Array name acquires actual memory address value
• int[] someNums = new int[10];
– Each element of someNums has value of 0
• char array elements
– Assigned ‘\u0000’
• boolean array elements
– Automatically assigned value false

Java Programming, Sixth Edition 8


Initializing an Array (cont'd.)
• Assign nondefault values to array elements upon
creation
int[] tenMult = {10, 20, 30, 40, 50,
60};
• Populating the array
– Providing values for all the elements in an array

Java Programming, Sixth Edition 9


Using Subscripts with an Array
• Scalar
– Primitive variable
• 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;

Java Programming, Sixth Edition 10


Using Subscripts with an Array
(cont'd.)
• Application contains array
– Use every element of array in some task
– Perform loops that vary loop control variable
• Start at 0
• End at one less than size of array
• Convenient to declare symbolic constant equal to
size of array
final int NUMBER_OF_SCORES = 5;

Java Programming, Sixth Edition 11


Using Subscripts with an Array
(cont'd.)
• Field
– Instance variable
– Automatically assigned value for every array created
• length field
– Contains number of elements in array
for(sub = 0; sub < scoreArray.length;
++sub)
scoreArray[sub] += 3;

Java Programming, Sixth Edition 12


Using Subscripts with an Array
(cont'd.)
• Enhanced for loop
– Cycle through array
– Without specifying starting and ending points for loop
control variable
for(int val : scoreArray)
System.out.println(val);

Java Programming, Sixth Edition 13


Declaring and Using Arrays of Objects
• Create array of Employee objects
Employee[] emp = new Employee[7];
– Must call seven individual constructors
final double PAYRATE = 6.35;
for(int x = 0; x < NUM_EMPLOYEES; ++x)
emp[x] = new Employee(101 + x, PAYRATE);

Java Programming, Sixth Edition 14


Using the Enhanced for Loop
• Use the enhanced for loop to cycle through an
array of objects
– Eliminates the need to use a limiting value
– Eliminates the need for a subscript following each
element
for(Employee worker : emp)
System.out.println(worker.getEmpNum() +
" " + worker.getSalary();

Java Programming, Sixth Edition 15


Manipulating Arrays of Strings
• Create array of Strings
String[] deptName = {"Accounting",
"Human Resources", "Sales"};
for(int a = 0; a < deptName.length; +
+a)
System.out.println(deptName[a]);

Java Programming, Sixth Edition 16


Manipulating Arrays of Strings
(cont'd.)

Java Programming, Sixth Edition 17


Searching an Array
• Determine whether variable holds one of many
valid values
– Use series of if statements
– Compare variable to series of valid values

Java Programming, Sixth Edition 18


Searching an Array (cont'd.)
• Searching an array
– Compare variable to list of values in array
for(int x = 0; x < validValues.length;
++x)
{
if(itemOrdered == validValues[x])
validItem = true;
}

Java Programming, Sixth Edition 19


Searching an Array (cont'd.)
• Parallel array
– One with same number of elements as another
– Values in corresponding elements related
• Alternative for searching
– Use while loop

Java Programming, Sixth Edition 20


Java Programming, Sixth Edition 21
Searching an Array (cont'd.)

Java Programming, Sixth Edition 22


Searching an Array For
a Range Match
• Searching array for exact match
– Not always practical
• Range match
– Compare value to endpoints of numerical ranges
– Find category in which value belongs

Java Programming, Sixth Edition 23


Java Programming, Sixth Edition 24
Passing Arrays to and Returning
Arrays from Methods
• Pass single array element to method
– Same as passing variable
• Passed by value
– Copy of value made and used in receiving method
– All primitive types passed this way

Java Programming, Sixth Edition 25


Passing Arrays to Methods (cont'd.)
• Reference types
– Object holds memory address where values stored
– Receiving method gets copy of array’s actual
memory address
– Receiving method has ability to alter original values
in array elements

Java Programming, Sixth Edition 26


Java Programming, Sixth Edition 27
Returning an Array from a Method
• Method can return an array reference
• Include square brackets with the return type
– In the method header

Java Programming, Sixth Edition 28


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

Java Programming, Sixth Edition 29


You Do It (cont'd.)
• Creating an interactive application that creates an
array of objects
• Passing an array to a method

Java Programming, Sixth Edition 30


Don’t Do It
• Don’t forget that the lowest array subscript is 0
• Don’t forget that the highest array subscript is one
less than the length
• Don’t forget that length is an array property and not
a method
• Don’t place a subscript after an object’s field or
method name when accessing an array of objects
• Don’t assume that an array of characters is a string

Java Programming, Sixth Edition 31


Don’t Do It (cont'd.)
• Don’t forget that array names are references
• Don’t use brackets with an array name when you
pass it to a method

Java Programming, Sixth Edition 32


Summary
• Array
– Named list of data items
– All have same type
• Array names
– Represent computer memory addresses
• Shorten many array-based tasks
– Use variable as subscript
• length field
– Contains number of elements in array

Java Programming, Sixth Edition 33


Summary (cont'd.)
• You can declare arrays that hold elements of any
type, including Strings and other objects
• Search array to find match to value
• Perform range match
• Pass single array element to method

Java Programming, Sixth Edition 34

You might also like