1 / 17

Arrays

Arrays. Chapter 7. Declaring arrays. Stepping through arrays. Passing arrays as parameters. Inspecting arrays. "All students to receive arrays!" reports Dr. Austin. scores : 85 79 92 57 68 80 . . . . 0 1 2 3 4 5 98 99.

dixie
Télécharger la présentation

Arrays

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. Arrays Chapter 7

  2. Declaring arrays Stepping through arrays Passing arrays as parameters Inspecting arrays "All students to receive arrays!" reports Dr. Austin. scores : 85 79 92 57 68 80 . . . 0 1 2 3 4 5 98 99

  3. Note the difference in the declaration - the number of elements is not specified in the first set of square brackets. Declaring and Creating Arrays • Recall that an array is a collection of elements all of the same type • Array objects in Java must be created with the key word new • Syntaxint c[] = new int [12]; • Results: • 12 integer locations are allocated • They are initialized to 0(null for reference variables, false for boolean)

  4. Using Arrays • View example Figure 7.2 • Note • Declaration of the array • Allocation of memory with new • Initialization of array elements • Use of array attribute (length) • Printing of array contents

  5. The Array Initializer • A comma separated list of expressions • The initializer list • Enclosed in braces • View the Example • Note again, how the array knows its own lengthfor (int count = 0 ; count < array.length ; count++) . . .

  6. On Average People Are Mean Processing Elements of An Array • Finding and using the arithmetic mean • Declare the array, create • Read in values, count how many • Declare a summation variable, initialize • Sum the array, calculate average • Sort the array • Print results, accessing the array • See sample program

  7. Histogram Program • Use the value in the array to • Print the value • Print a string of asterisks for visualization of size of the value • View Figure 7.6,

  8. Using Array Elements as Counters • Recall program which tested random number generator (Figure 6.8) • Used 6 different variables • Used switch statement to increment • At the time we noted the inefficiency • Consider a new version of the program • Uses array elements to total the number of rolls of each number 1 – 6 • No switch statement needed • View Figure 7.7

  9. Enhanced for Statement • Enhanced for statement • New feature of J2SE 5.0 • Allows iterates through elements of an array or a collection without using a counter • Syntax for ( parameter : arrayName ) statement • View example, Figure 7.12

  10. 17 Object References and Reference Parameters • In Java, primitive-type variables are always passed by value • Incoming data only • Objects are not passed to methods • References to objects are passed • The reference is passed by value • With a reference to the object the method can manipulate the object directly Address of object

  11. References and Reference Parameters • Passing arrays by reference • Improves performance • Saves time • Initialization of large array not required • Saves memory • Use memory already occupied by array • No new memory allocation required • View example program, Figure 7.13

  12. Using Arrays • Sorting Arrays • Recall previous example • Searching Arrays • Linear search (see example) • Step through array until desired value located • Binary search • Array must be sorted • Look in middle, then above or below • Look in middle of sub-section then above or below • Etc.

  13. Multidimensional Arrays • Java does not support multidimensional arrays directly • Does allow declaration of arrays whose elements are arrays! int b[][];b = new int[ 2 ][ ]; // allocate rowsb[ 0 ] = new int[ 5 ]; // allocate columns for row 0b[ 1 ] = new int[ 3 ]; // allocate columns for row 1

  14. Fig. 7.16| Two-dimensional array with three rows and four columns. Multidimensional Arrays

  15. Creating Two-dimensional Arrays • Can be created dynamically • 3-by-4 array int b[][]; b = newint[ 3 ][4 ]; • Rows can have different number of columns int b[][]; b = newint[ 2 ][ ]; // create 2 rows b[ 0 ] = newint[ 5 ]; // create 5 columns for row 0 b[ 1 ] = newint[ 3 ]; // create 3 columnsfor row View ExampleFigure 7.17

  16. Variable-Length Argument Lists • New feature in J2SE 5.0 • Unspecified number of arguments • Use ellipsis (…) in method’s parameter list • Can occur only once in parameter list • Must be placed at the end of parameter list • Array whose elements are all of the same type • View example, Fig. 7.20

  17. Using Command-Line Arguments • Pass arguments from the command line • String args[] • Appear after the class name in the java command • java MyClass a b • Number of arguments passed in from command line • args.length • First command-line argument • args[ 0 ] View ExampleFigure 7.21

More Related