Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that

Embed Size (px)

DESCRIPTION

Objectives, cont. (optional) learn about text fields and text areas in applets (optional) learn to draw arbitrary polygons and polylines in applets

Citation preview

Arrays Chapter 6 Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that return an array learn the proper use of an array as an instance variable learn about multidimensional arrays Objectives, cont. (optional) learn about text fields and text areas in applets (optional) learn to draw arbitrary polygons and polylines in applets Outline Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays (optional) Graphics Supplement Introduction to Arrays An array is an object used to store a (possibly large) collection of data. All the data stored in the array must be of the same type. An array object has a small number of predefined methods. Introduction to Arrays, cont. Sometimes you want to know several things about a collection of data: the average the number of items below the average the number of items above the average etc. This requires all the items to be stored as variables of one kind or another. Introduction to Arrays, cont. Arrays satisfy this need. Even though an array is an object in Java, it can be convenient to think of it as a collection of variables of the same type. Array Basics: Outline Creating and Accessing Arrays Array Details The length Instance Variable Initializing Arrays Creating and Accessing Arrays example double[] temperature = new double[7]; is like declaring seven variables of type double, named temperature[0], temperature[1], temperature[2], temperature[3], temperature[4], temperature[5], temperature[6]. Creating and Accessing Arrays, cont. These variables can be used just like any other variables of type double. examples temperature[3] = 32.0; temperature[6] = temperature[3] + 5; System.out.println(temperature[6]); temperature[index+1] = 66.5; These variables are called indexed variables, elements, or subscripted variables. Creating and Accessing Arrays, cont. The integer expression within the square brackets is called the index (or subscript). Creating and Accessing Arrays, cont. class ArrayOfTemperatures Creating and Accessing Arrays, cont. Array Details syntax for creating an array Base_Type[] Array_Name = new Base_Type[Length]; example int[] pressure = new int[100]; or int[] pressure; pressure = new int[100]; Array Details, cont. The type of the elements is called the base type. The base type of an array can be any type including a class type. for example, Species[] entry = new Species[3]; The number of elements is the length or size of the array. Brackets [] Brackets [] serve three purposes: creating the type name example: int[] pressure; creating the new array pressure = new int[100]; naming an indexed variable of the array pressure[3] = keyboard.nextInt(); Array Terminology Singular Array Names Whether an array holds singular primitive types or singular class types, singular names make the code more self-documenting. for example, entry[2] makes more sense than entries[2]. The length Instance Variable An array has only one public instance variable, namely length. The length variable stores the number of elements the array can hold. Using Array_Name.length typically produces clearer code than using an integer literal. The length Instance Variable, cont class ArrayOfTemperatures2 Indices and length The indices of an array start with 0 and end with Array_Name.length-1. When a for loop is used to step through an array, the loop control variable should start at 0 and end at length-1. example for (lcv = 0; lcv < temperature.length; index++) Array Index Out of Bounds Every index must evaluate to an integer which is not less than 0 and not greater than Array_Name.length-1. Otherwise, the index is said to be out of bounds or invalid. An out-of-bounds index will produce a run- time error. Incomplete Array Processing Loops fail to process an entire array correctly when they begin with an index other than 0 end with an index other than length-1. Examples for (i = 1; i < oops.length-1; index++) for (i = 1; i