Java Programming,: Second Edition Chapter Eight Arrays

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 30

Java Programming, Second Edition

Chapter Eight
Arrays
In this chapter, you will:

• Declare and initialize an array


• Use subscripts with an array
• Declare an array of objects
• Search an array for an exact match
or a range match
• Pass arrays to methods
• Use the length field
• Create arrays of Strings
• Sort primitive, object, and String array
elements
• Use two-dimensional and multidimensional
arrays
Declaring and Initializing an Array

• Array- Named list of data items that all


have the same type
– Holds a memory address where a value is
stored; in other words an array name is a
reference
– int[] someNums;
Initializing an Array
• By default, character array elements are assigned ‘\u0000’
• Boolean array elements are automatically assigned false
• To initialize an array, you use a list of values separated by
commas and enclosed within curly braces

int[] tenMult = {10, 20, 30, 40, 50, 60};

• When you initialize an array by giving it values upon


creation, you do not give the array a size; the size will be
assigned based on the number of values you place in the
initializing list
Using Subscripts with an Array
• If you treat each array element as an
individual entity, there is no advantage over
primitive variables
• The power of arrays becomes apparent
when you use subscripts that are variables,
rather than subscripts that are constant
values
Using Subscripts with an Array
• Int[] priceArray = {2, 14, 35,67, 85}
– To increase each price array element by 3 dollars, you
can write the following
priceArray[0] += 3;
priceArray[1] += 3;
priceArray[2] += 3;
priceArray[3] += 3;
priceArray[4] += 3;
priceArray[5] += 3;
• Or, you can shorten the task by using a
variable as the subscript
• Then you can use a loop to perform
arithmetic on each array element
for(sub = 0; sub < 5; ++sub)
priceArray[sub] += 3;
Declaring an Array of Objects
• Similar to declaring arrays of integers or
doubles, you can declare arrays that hold
elements of any type including objects
• To use a method that belongs to an object
that is part of the array, you insert the
appropriate subscript notation after the
array name and before the dot that precedes
the method name
Searching an Array for an Exact
Match
• When you want to determine whether some variable holds
one of many valid values, one option is to use a series of if
statements to compare the variable to a series of valid
values
• Or you can compare the variable to a list of values in an
array
– The for loop can replace a long series of if statements
– In an array with many possible matches, it is more
efficient to place the more common items first, so they
are matched right away
Searching an Array for an Exact
Match
• You can also use a while loop to search for
a match
– Set the subscript to zero and increase the
subscript while searching through the items
Using the while Loop
Searching an Array for a Range
Match
• Searching an array for an exact match is not
always practical
Example:
– Your company discounts on the quantity of
items ordered
– There are increasing discounts for orders of
increasing quantities
Searching an Array for an Exact
Match
• One option is to create a single array to
store the discount rates
• A better option is to create parallel arrays
– One array would hold discount rates
– The other array would hold discount range
limits
Passing Arrays to Methods
• You can pass a single array element to a
method in exactly the same way as you pass
a variable
• The variables are local to the method and
any changes to variables passed into
methods are not permanent
– Changes are not reflected in the main() program
Passing Arrays to Methods
• Arrays are passed by reference
– The method receives the memory address of the
array and has access to the actual values in the
array elements
Using the Length Field
• Every object you create is automatically
assigned a data field named length
• Length field- Contains the number of
elements in the array
– Ensure that the subscript you use remains in the
range zero through one less the length
Creating Arrays of Strings
• You can create an array of Strings
– For example:
String[] deptName = {“Accounting”,
“Human Resources”, “Sales”,};
Sorting Primitive, Object, and String
Array Elements
• Sorting- The process of arranging a series
of objects in some logical order
– Ascending – starting with the object with the
lowest value
– Descending – starting with the object that has
the largest value
Sorting Primitive, Object, and String
Array Elements
• To sort any two values in ascending order,
use an if statement to make the decision to
swap the values
The Bubble Sort
• To use a bubble sort, you place the
original, unsorted values in an array
• Compare pairs of values
• If they are not in ascending order, swap them
• At most, you will have to pass the list as
many times as its length minus one
Sorting Primitive, Object, and String
Array Elements
• String names are addresses
• You must use the compareTo() method to
when sorting Strings
Single-Dimensional Arrays
• Single-dimensional array- An array that you
can picture as a column of values
Using Two-Dimensional and
Multidimensional Arrays
Two-dimensional arrays
• Have more than one column of values
• Other terms for a two-dimensional array are:
– Matrix
– Spreadsheet
• String[][] students =
{ {“Dave”, “Bonnie”, “Hannah”},
{“Iris”, “Keith”, “Carl”}
{“Amy”, “Jessica”, “Francis”}
{“Ellen”, “George”, “Lydia”} };
Multidimensional Arrays
• Multidimensional arrays- Arrays of more
than two dimensions

You might also like