java programming second edition l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Java Programming, Second Edition PowerPoint Presentation
Download Presentation
Java Programming, Second Edition

Loading in 2 Seconds...

play fullscreen
1 / 29

Java Programming, Second Edition - PowerPoint PPT Presentation


  • 108 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Java Programming, Second Edition' - cady


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
in this chapter you will
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
slide3
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
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
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
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 array7
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;

slide8
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
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

empArray[1].getName(); // get emp1 name

empArray[7].setSalary(30000);

searching an array for an exact match
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 match13
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
searching an array for a range match
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 match16
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
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 methods19
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
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
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 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 elements23
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
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 elements27
Sorting Primitive, Object, and String Array Elements
  • String names are addresses
  • You must use the compareTo() method to when sorting Strings
arrays
Arrays
  • Single-dimensional array- An array that you can picture as a column of values
  • Two-dimensional arrays – Arrays with multiple rows & columns. The row is always referenced first.
  • Multidimensional arrays- Arrays of more than two dimensions. Adding subcolumns under each column.
using two dimensional and multidimensional arrays
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”} };