1 / 29

Java Programming, Second Edition

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

cady
Download Presentation

Java Programming, Second Edition

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming, Second Edition Chapter Eight Arrays

  2. 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

  3. 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

  4. 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;

  5. 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

  6. 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

  7. 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;

  8. 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;

  9. 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);

  10. 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

  11. 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

  12. Using the while Loop

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. Creating Arrays of Strings • You can create an array of Strings • For example: String[] deptName = {“Accounting”, “Human Resources”, “Sales”,};

  19. 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

  20. 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

  21. 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

  22. Sorting Primitive, Object, and String Array Elements • String names are addresses • You must use the compareTo() method to when sorting Strings

  23. 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.

  24. 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”} };

More Related