ch10 - Chapter 10 Arrays Topics Declaring and instantiating...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 10 Arrays
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Topics Declaring and instantiating arrays Array element access Arrays of objects Arrays as method parameters Arrays as return values Command-line arguments
Background image of page 2
Array Basics In Java, an array is an indexed collection of data values of the same type. Arrays are useful for sorting and manipulating a collection of values.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example: Monthly Rainfall Monthly rainfall averages and their variation from the annual average. Annual average rainfall: 15.03 mm Month Average Variation 1 13.3 1.73 2 14.9 0.13 3 14.7 0.33 4 23.0 7.97 5 25.8 10.77 6 27.7 12.67 7 12.3 2.73 8 10.0 5.03 9 9.8 5.23 10 8.7 6.33 11 8.0 7.03 12 12.2 2.83
Background image of page 4
Declaring Arrays Square brackets [ ] are used to declare an array. To declare an array, we may attach the brackets to either the data type or the variable name. An array named rainfall of type double , may be stated either as double [] rainfall; double rainfall [];
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Instantiating Arrays In Java, an array is a reference data type. Objects are also reference data types As with objects, the declaration only allocates memory for the reference (memory address) We use the new operator to allocate the memory to store the values in an array. rainfall = new double [12]; //creates an array of size 12.
Background image of page 6
Indexing Array Elements We use a single identifier to refer to the whole collection in the array. We use an indexed expression to refer to the individual values of the collection. Arrays use zero- based indexing. An individual value in an array is called an array element .
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Assigning values to array elements The elements of a numeric array initially have the value 0. To be useful, each element needs to be assigned a value. Loops are very useful for processing arrays An array of 12 double values after all 12 are assigned values.
Background image of page 8
How big should we make it? Declaration of arrays with a constant for the
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 27

ch10 - Chapter 10 Arrays Topics Declaring and instantiating...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online