Download presentation
Presentation is loading. Please wait.
1
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays
2
© The McGraw-Hill Companies, 2006 ARRAYS How to create and handle a very large number of data items?
3
© The McGraw-Hill Companies, 2006 How to enter 7 temperature readings? Try using a for loop?
4
© The McGraw-Hill Companies, 2006 What is an array? An array is a data type that stores a collection of items. These items are sometimes referred to as the elements of the array. All elements must be of the same type BUT there is no restriction on which type this is. For example arrays can be used to hold a collection of int values; or a collection of char values; BUT they cannot be used to hold a mixture of int and char values.
5
© The McGraw-Hill Companies, 2006 Declaring an array variable Example: an array to hold a collection of integer variables: int[] someArray; So, to declare an array temperature containing double values:
6
© The McGraw-Hill Companies, 2006 Allocating memory to store the array elements Need to state –the size of the array ; –the type of each individual array element. The array type and size are then put together with a special new operator. Example: an array of 10 integers: arrayName = new int[10];
7
© The McGraw-Hill Companies, 2006 Returning to the temperature array The temperature array holds seven double values: The two stages of array creation can be combined into one step as follows:
8
© The McGraw-Hill Companies, 2006 The effect on computer memory of declaring an array
9
© The McGraw-Hill Companies, 2006 Naming the array elements The first element in the temperature array is temperature[0] The second element is temperature[1] and so on:
10
© The McGraw-Hill Companies, 2006 Initializing an array This is the only instance in which all the elements of an array can be assigned explicitly by listing out the elements in a single assignment statement.
11
© The McGraw-Hill Companies, 2006 Accessing array elements Array can be used like any other variable of the given type in Java. The assignment operator can be used to enter a value. You must specify which element to place the value in. For example Allowing the user of the program to enter the value of the first temperature:
12
© The McGraw-Hill Companies, 2006 Using a variable as the array index Example: Assume that i is some integer variable:
13
© The McGraw-Hill Companies, 2006 Using a loop counter as an array index Example Entering all seven temperature readings:
14
© The McGraw-Hill Companies, 2006 The length attribute The length attribute returns the size of an array. It is accessed by using the word length after the name of the array.
15
© The McGraw-Hill Companies, 2006 Passing arrays as parameters Program 5.1 contains all the processing within the main method. Now we will create two helper methods, enterTemps and displayTemps, to enter and display temperatures respectively. Example: the code for enterTemps
16
© The McGraw-Hill Companies, 2006 The main method
17
© The McGraw-Hill Companies, 2006 The effect on computer memory of passing an array as a parameter
18
© The McGraw-Hill Companies, 2006 Returning an array from a method
19
© The McGraw-Hill Companies, 2006 Modifying the main method We need to modify the main method so that the returned array value is used to set the value of the original temperature array:
20
© The McGraw-Hill Companies, 2006 The enhanced 'for' loop Example To display on the screen each value from the temperature array: The loop header is to be read as “for each item in the temperature array”.
21
© The McGraw-Hill Companies, 2006 When to use the enhanced 'for' loop? You should use an enhanced for loop only when: –you wish to access the entire array (and not just part of the array); –you wish to read the elements in the array, not modify them; –you do not require the array index for additional processing.
22
© The McGraw-Hill Companies, 2006 Array maximum: design
23
© The McGraw-Hill Companies, 2006 Array maximum: implementation
24
© The McGraw-Hill Companies, 2006 Array maximum: an alternative implementation
25
© The McGraw-Hill Companies, 2006 Array summation: design
26
© The McGraw-Hill Companies, 2006 Array summation: implementation
27
© The McGraw-Hill Companies, 2006 Array membership: design
28
© The McGraw-Hill Companies, 2006 Array membership: implementation
29
© The McGraw-Hill Companies, 2006 Array search: design
30
© The McGraw-Hill Companies, 2006 Array search: implementation
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.