ArraysDeclaring and Creating ArraysUsing ArraysThe Array InitializerProcessing Elements of An ArrayHistogram ProgramUsing Array Elements as CountersEnhanced for StatementReferences and Reference ParametersSlide 10Slide 11Multidimensional ArraysFig. 7.16 | Two-dimensional array with three rows and four columns.Creating Two-dimensional ArraysVariable-Length Argument ListsUsing Command-Line ArgumentsArraysChapter 72Declaring and Creating Arrays•Recall that an array is a collection of elements all of the _____________•Array objects in Java must be created with the key word _______________•Syntaxint c[] = new int [12];•Results:–12 integer locations are allocated–They are _____________________(null for reference variables, false for boolean)3Using Arrays•View example Figure 7.2•Note–Declaration of the array–Allocation of memory with new–Initialization of array elements–Use of array attribute (_______________)–Printing of array contents4The Array Initializer•A comma separated list of expressions–The ______________ list–Enclosed in _________________•View the Example–Note again, how the array knows its own _______for (int count = 0 ; count < array.length ; count++) . . .5Processing Elements of An Array•Finding and using the arithmetic mean–__________________ the array, create–Read in values, ___________________–Declare a summation variable, initialize –______________ the array, calculate average–_______________ the array–Print results, accessing the array•See sample program6Histogram Program•Use the value in the array to–Print the value–Print a string of asterisks for __________________ of size of the value•View Figure 7.6,7Using 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 ________________•Consider a new version of the program–Uses _______________ to total the number of rolls of each number 1 – 6–No ___________ statement needed•View Figure 7.78Enhanced for Statement•Enhanced for statement–New feature of J2SE 5.0–Allows iterates through elements of an array or a collection _______________________–Syntaxfor ( parameter : arrayName )stat ement–View example, Figure 7.129References and Reference Parameters•In Java, primitive-type variables are always passed by _________–Incoming data only•Objects are not passed to methods–__________ to objects are passed–The reference is passed by value•With a reference to the object the method can _____________ the object directly10References and Reference Parameters•Passing arrays by reference–Improves _______________–Saves time•________________ of large array not required–Saves _______________•Use memory already occupied by array•No new memory allocation required–View example program, Figure 7.1311Using Arrays•Sorting Arrays–Recall previous example•Searching Arrays–________________ search (see example)•Step through array until desired value located–Binary search•Array must be _________________•Look in middle, then above or below–Look in middle of sub-section then above or below–Etc.12Multidimensional Arrays•Java does not support ________________ arrays directly•Does allow declaration of arrays whose elements are ______________!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 113Fig. 7.16 | Two-dimensional array with three rows and four columns. Multidimensional Arrays14Creating Two-dimensional Arrays–Can be created dynamically–3-by-4 array int b[][]; b = new int[ 3 ][ 4 ];–Rows can have different number of ____________ int b[][]; b = new int[ 2 ][ ]; // create 2 rows b[ 0 ] = new int[ 5 ]; // create 5 columns for row 0 b[ 1 ] = new int[ 3 ]; // create 3 columns for row15Variable-Length Argument Lists•New feature in J2SE 5.0•___________________ number of arguments•Use ellipsis (…) in method’s parameter list–Can occur _______________ in parameter list–Must be placed at the __________ of parameter list•Array whose elements are all of the same type•View example, Fig. 7.2016Using Command-Line Arguments•Pass arguments from the command line–String args[]•Appear after the _____________ in the java command–java MyClass a b•_________________ of arguments passed in from command line–args.length•First command-line argument–args[ 0
View Full Document