Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.

Similar presentations


Presentation on theme: " 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4."— Presentation transcript:

1  2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4

2  2005 Pearson Education, Inc. All rights reserved. 2 Introduction Arrays – Data structures – Related data items of same type – Remain same size once created Fixed-length entries Array – Group of variables Have same type – Reference type

3  2005 Pearson Education, Inc. All rights reserved. 3 An index must be an int value or a value of a type that can be promoted to int—namely, byte, short or char, but not long. A 12-element array.

4  2005 Pearson Education, Inc. All rights reserved. 4 Declaring and Creating Arrays Declaring and Creating arrays – Arrays are objects that occupy memory – Created dynamically with keyword new int c[] = new int[ 12 ]; – Equivalent to int c[]; // declare array variable c = new int[ 12 ]; // create array We can create arrays of objects too String b[] = new String[ 100 ]; Array of string objects int c[ 12 ];

5  2005 Pearson Education, Inc. All rights reserved. 5 Examples Using Arrays Creating and initializing an array – Declare array – Create array – Initialize array elements

6  2005 Pearson Education, Inc. All rights reserved. 6 Outline Declare array as an array of int s Create 10 int s for array ; each int is initialized to 0 by default array.length returns length of array array[counter] returns int associated with index in array Each int is initialized to 0 by default

7  2005 Pearson Education, Inc. All rights reserved. 7 Examples Using Arrays (Cont.) Using an array initializer – Use initializer list Items enclosed in braces ( {} ) Items in list separated by commas int n[] = { 10, 20, 30, 40, 50 }; – Creates a five-element array – Index values of 0, 1, 2, 3, 4 – Do not need keyword new

8  2005 Pearson Education, Inc. All rights reserved. 8 Outline InitArray.java Line 9 Declare array as an array of ints Line 9 Compiler uses initializer list to allocate array Program output Declare array as an array of int s Compiler uses initializer list to allocate array

9  2005 Pearson Education, Inc. All rights reserved. 9 Examples Using Arrays (Cont.) Calculating a value to store in each array element – Initialize elements of 10-element array to even integers

10  2005 Pearson Education, Inc. All rights reserved. 10 Outline InitArray.java Line 8 Declare constant variable Line 9 Declare and create array that contains 10 ints Line 13 Use array index to assign array Program output Declare constant variable ARRAY_LENGTH using the final modifier Declare and create array that contains 10 int s Use array index to assign array value

11  2005 Pearson Education, Inc. All rights reserved. 11 Error-Prevention Tip When writing code to loop through an array, ensure that the array index is always greater than or equal to 0 and less than the length of the array. The loop-continuation condition should prevent the accessing of elements outside this range.

12  2005 Pearson Education, Inc. All rights reserved. 12 Outline EnhancedForTest.java For each iteration, assign the next element of array to int variable number, then add it to total Lines 12-13 are equivalent to for ( int counter = 0; counter < array.length; counter++ ) total += array[ counter ]; Usage Can access array elements Cannot modify array elements Cannot access the counter indicating the index

13  2005 Pearson Education, Inc. All rights reserved. 13 Passing Arrays to Methods To pass array argument to a method – Specify array name without brackets Array hourlyTemperatures is declared as int hourlyTemperatures [ ] = new int[ 24 ]; The method call modifyArray( hourlyTemperatures ); Passes array hourlyTemperatures to method modifyArray

14  2005 Pearson Education, Inc. All rights reserved. Passing Arrays to Methods Remember that when an array is passed to a method, only its reference is passed. A copy of the array is not created in the method.

15  2005 Pearson Education, Inc. All rights reserved. Fig1. A Passing an array to a method means we are passing a reference to an array.

16  2005 Pearson Education, Inc. All rights reserved. Fig1. B Passing an array to a method means we are passing a reference to an array.

17  2005 Pearson Education, Inc. All rights reserved. Fig1. C Passing an array to a method means we are passing a reference to an array.

18  2005 Pearson Education, Inc. All rights reserved. Fig1. D Passing an array to a method means we are passing a reference to an array.

19  2005 Pearson Education, Inc. All rights reserved. Return Arrays from Methods Next we will consider an example in which return an array from a method. This method inputs double values and returns the values as an array of double.

20  2005 Pearson Education, Inc. All rights reserved. public double[] readDoubles() { double[] number; String input = JOptionPane.showInputDialog(null, "How many input values?"); int N = Integer.parseInt(input); number = new double[N]; for (int i = 0; i<N; i++){ String inputD = JOptionPane.showInputDialog(null, "Number " + i); number[i] = Double.parseDouble(inputD); } return number; } Return Arrays from Methods

21  2005 Pearson Education, Inc. All rights reserved. 21

22  2005 Pearson Education, Inc. All rights reserved. Return Arrays from Methods The readDoubles method is called: double[] arrayOne; //assign values to arrayOne arrayOne = readDoubles(); Because a new array is created by the method, we do not have to create an array from the calling side. Doing so will not cause an error, but it is a wasteful operation.

23  2005 Pearson Education, Inc. All rights reserved. Fig2. A The effect of creating a local array and not returning it.

24  2005 Pearson Education, Inc. All rights reserved. Fig2. B The effect of creating a local array and not returning it.

25  2005 Pearson Education, Inc. All rights reserved. Fig2. C The effect of creating a local array and not returning it.

26  2005 Pearson Education, Inc. All rights reserved. Fig2. D The effect of creating a local array and not returning it.

27  2005 Pearson Education, Inc. All rights reserved. 27 Passing Arrays to Methods (Cont.) Notes on passing arguments to methods – Two ways to pass arguments to methods Pass-by-value – Copy of argument’s value is passed to called method – In Java, every primitive is pass-by-value Pass-by-reference – Caller gives called method direct access to caller’s data – Called method can manipulate this data – Improved performance over pass-by-value – In Java, every object is pass-by-reference In Java, arrays are objects Therefore, arrays are passed to methods by reference

28  2005 Pearson Education, Inc. All rights reserved. 28 Performance Tip Passing arrays by reference makes sense for performance reasons. If arrays were passed by value, a copy of each element would be passed. For large, frequently passed arrays, this would waste time and consume considerable storage for the copies of the arrays.

29  2005 Pearson Education, Inc. All rights reserved. 29 Outline PassArray.java (1 of 2) Line 9 Line 19 Declare 5 - int array with initializer list Pass entire array to method modifyArray

30  2005 Pearson Education, Inc. All rights reserved. 30 Outline PassArray.java (2 of 2) Line 30 Lines 36-40 Lines 43-48 Program output Pass array element array[3] to method modifyElement Method modifyArray manipulates the array directly Method modifyElement manipulates a primitive’s copy

31  2005 Pearson Education, Inc. All rights reserved. 31 Case Study:

32  2005 Pearson Education, Inc. All rights reserved. 32

33  2005 Pearson Education, Inc. All rights reserved. 33 Case Study: Class GradeBook Using an Array to Store Grades [student study] Further evolve class GradeBook Class GradeBook – Represent a grade book that stores and analyzes grades – Does not maintain individual grade values – Repeat calculations require reentering the same grades Can be solved by storing grades in an array

34  2005 Pearson Education, Inc. All rights reserved. 34 Outline GradeBook10.jav a (1 of 5) Line 7 Line 13 Declare array grades to store individual grades Assign the array’s reference to instance variable grades

35  2005 Pearson Education, Inc. All rights reserved. 35 Outline GradeBook10.java (2 of 5)

36  2005 Pearson Education, Inc. All rights reserved. 36 Outline GradeBook10.java (3 of 5) Lines 59-64 Lines 75-80 Loop through grades to find the lowest grade Loop through grades to find the highest grade

37  2005 Pearson Education, Inc. All rights reserved. 37 Outline GradeBook10.java (4 of 5) Lines 91-92 Lines 107-108 Loop through grades to sum grades for one student Loop through grades to calculate frequency

38  2005 Pearson Education, Inc. All rights reserved. 38 Outline GradeBook10.java (5 of 5) Lines 134-136 Loop through grades to display each grade

39  2005 Pearson Education, Inc. All rights reserved. 39 Outline GradeBookTest10.java (1 of 2) Declare and initialize gradesArray with 10 elements Pass gradesArray to GradeBook constructor

40  2005 Pearson Education, Inc. All rights reserved. 40 Outline GradeBookTest.java (2 of 2) Program output

41  2005 Pearson Education, Inc. All rights reserved. 41 Examples

42  2005 Pearson Education, Inc. All rights reserved. 42

43  2005 Pearson Education, Inc. All rights reserved. 43 StudentSample_A

44  2005 Pearson Education, Inc. All rights reserved. 44 StudentSample_A

45  2005 Pearson Education, Inc. All rights reserved. 45 StudentSample_A

46  2005 Pearson Education, Inc. All rights reserved. 46 StudentSample_A

47  2005 Pearson Education, Inc. All rights reserved. 47 StudentSample_A

48  2005 Pearson Education, Inc. All rights reserved. 48

49  2005 Pearson Education, Inc. All rights reserved. 49 StudentSample_B 1 st constructor 2 nd constructor

50  2005 Pearson Education, Inc. All rights reserved. 50 StudentSample_B

51  2005 Pearson Education, Inc. All rights reserved. 51 StudentSample_B

52  2005 Pearson Education, Inc. All rights reserved. 52 StudentSample_B

53  2005 Pearson Education, Inc. All rights reserved. 53 StudentSample_B

54  2005 Pearson Education, Inc. All rights reserved. 54 StudentSample_B

55  2005 Pearson Education, Inc. All rights reserved. 55


Download ppt " 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4."

Similar presentations


Ads by Google