ch12 - Alice in Action with Java Chapter 12 Arrays and...

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Alice in Action with Java Chapter 12 Arrays and Lists in Java Alice in Action with Java 2 Objectives • Understand Java’s array data structure • Solve problems using Java’s LinkedList data structure • Solve problems using Java’s ArrayList data structure Alice in Action with Java 3 Arrays and Lists in Java • Features common to arrays and lists – Used to define variables that store groups of items – Provide access to a given item using an index • Differences between arrays and lists – Array’s size is fixed at runtime, a list’s size can change – Array stores items using less memory than a list – Array provides direct item access faster than a list Alice in Action with Java 4 Introductory Example: Air Pollution Reporting • Review of AirPollutionIndex.java – Read air pollution level readings from five points – Compute and display the average reading (the index) • Additional requirements of the enhanced program – Display the five readings used to compute the average • High-level algorithm for AirPollutionReport – Build an array named readings with length = 5 – Read the air pollution readings into readings – Compute and display average of values in readings – Display the individual values in readings Alice in Action with Java 5 Introductory Example: Air Pollution Reporting (continued) Alice in Action with Java 6 Introductory Example: Air Pollution Reporting (continued) Alice in Action with Java 7 Java Arrays • General pattern for defining (declaring) an array – Item anArray = new Item[N]; • Item : specifies the items type • Brackets tell compiler that anArray is an array handle • new operator: allocates memory for the array • N : an integer that specifies the array length (size) • An example of an array definition: double readings = new double [NUM_READINGS]; – NUM_READINGS is an integer constant = 5 – readings is a handle to a 5 unit double type array Alice in Action with Java 8 Java Arrays (continued) • Element: indexed unit variable in an array • Item: value stored in an array element • Items are initialized to default values for array’s type – Example: default value for item in double type is 0.0 • Creating an array parameter – Place brackets between parameter’s type and its name – General form: public ReturnType methodName( Item parameterName ){… – Example: public static double average(double anArray) Alice in Action with Java 9 Java Arrays (continued) Alice in Action with Java 10 Java Arrays (continued) • Components needed to access an array’s elements – Array’s handle, item’s index, subscript operator ( ) • Pattern for accessing an element: anArray[i] – anArray is the handle to the array – i : index value, which must be a non-negative integer – An index value out of bounds throws an exception • Example of an array access: readings[0] – Accesses the first element of the readings array – Note: index is off by one relative to item’s cardinal order • length property: returns number of items in an array Alice in Action with Java...
View Full Document

{[ snackBarMessage ]}

Page1 / 53

ch12 - Alice in Action with Java Chapter 12 Arrays and...

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

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