LETU COSC 2103 - Arrays (11 pages)

Previewing pages 1, 2, 3, 4 of 11 page document View the full content.
View Full Document

Arrays



Previewing pages 1, 2, 3, 4 of actual document.

View the full content.
View Full Document
View Full Document

Arrays

60 views


Pages:
11
School:
LeTourneau University
Course:
Cosc 2103 - Computer Science II
Computer Science II Documents

Unformatted text preview:

Arrays Chapter 7 Declaring and Creating Arrays Recall that an array is a collection of all of the same type Array objects in Java must be created with the key word Syntax Results 12 integer locations are allocated They are initialized to 0 null for reference variables false for boolean 2 Using Arrays View the adapted version of Figure 7 2 Note Declaration of the array of memory with new Initialization of array elements Use of array attribute Printing of array contents 3 The Array Initializer A comma separated list of expressions The list Enclosed in braces View the Example Note again how the array knows for int count 0 count array length count 4 Processing Elements of An Array Finding and using the arithmetic mean the array create Read in values Declare a variable initialize Sum the array calculate average the array Print results accessing the array 5 Histogram Program Uses the JOptionPane output Use the value in the array to Print the Print a string of asterisks for of size of the value View Figure 7 7 listen to author s explanation 6 Using Array Elements as Counters Recall program which tested random number generator Figure 6 7 Used 6 Used statement to increment At the time we noted the inefficiency Consider a new version of the program Uses to total the number of rolls of each number 1 6 statement needed View Figure 7 7 7 References and Reference Parameters In Java primitive type variables are always passed by 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 directly 8 References and Reference Parameters Passing arrays by reference Improves Saves Initialization of large array not required Saves Use memory already occupied by array No new memory allocation required View example program Figure 7 9 9 Using Arrays Sorting Arrays Recall previous example Searching Arrays search see example Step through array until desired value located Binary search Array must be



View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arrays and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?