CSC245 Lecture 5
CSC245 Lecture 5
Common programming error: In an array declaration, specifying the number of elements in the square
brackets of the declaration (e.g., int c[ 12 ];) is a syntax error.
Common programming error: Declaring multiple array variables in a single declaration can lead to subtle
errors.
Consider int[ ] a, b, c;.
If only a is intended to be an array variable, and b and c are intended to be individual int variables, then
this declaration is incorrect.
int a[ ], b, c; would achieve the desired result.
Index Value
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
Index Value
0 32
1 27
2 64
3 18
creates a 10-element array with index values 0, 1, 2, 4 95
…, 9 5 14
6 90
7 70
8 60
9 37
Face Frequency
1 988
2 963
3 1018
4 1041
5 978
6 1012
where arrayName is the array through which to iterate, and parameter has two parts:
a type (e.g. int) which must match the type of the elements in the array; and
an identifier which represents the successive values in the array on successive iterations.
The enhanced for header can be read concisely as "for each iteration, assign the next element of
arrayName to parameter, then execute the following statement".
The enhanced for statement can be used only to access array elements, however, it cannot be used to
modify elements or to access the index number of each array element.
If the latter, use the traditional counter-controlled for statement.
specify the name of the array without any 4 public class PassArray
brackets. 5 {
6 // main creates array and calls modifyArray and modifyElement
7 public static void main( String args[] )
8 {
Every array object knows its own length 9 int array[] = { 1, 2, 3, 4, 5 };
(via length field), thus, we need not pass 10
11 System.out.println(
the array length as an additional 12 "Effects of passing reference to entire array:\n" +
argument into a method. 13 "The values of the original array are:" );
14
When an argument to a method is an 15 // output original array elements
entire array or an individual array element 16 for ( int value : array )
of a reference type, the called method 17 System.out.printf( " %d", value );
18
receives a copy of the reference. 19 modifyArray( array ); // pass array reference
Modifying a reference type in the 20 System.out.println( "\n\nThe values of the modified array are:" );
called method modifies the original 21
22 // output modified array elements
value of that reference.
23 for ( int value : array )
24 System.out.printf( " %d", value );
25
26 System.out.printf(
27 "\n\nEffects of passing array element value:\n" +
28 "array[3] before modifyElement: %d\n", array[ 3 ] );
02/05/2019 DIANA HAIDAR 16
Passing Arrays to Methods 29
30 modifyElement( array[ 3 ] ); // attempt to modify array[ 3 ]
31 System.out.printf(
When an argument to a method is an 32 "array[3] after modifyElement: %d\n", array[ 3 ] );
individual array element of a primitive 33 } // end main
type, the called method receives a copy 34
35 // multiply each element of an array by 2
of the element's value. 36 public static void modifyArray( int array2[] )
Modifying the copy of the element's 37 {
value in the called method does not 38 for ( int counter = 0; counter < array2.length; counter++ )
39 array2[ counter ] *= 2;
affect the original value of that 40 } // end method modifyArray
element. 41
42 // multiply argument by 2
43 public static void modifyElement( int element )
44 {
45 element *= 2;
46 System.out.printf(
For a method to receive an array 47 "Value of element in modifyElement: %d\n", element );
reference through a method call, the 48 } // end method modifyElement
method's parameter list must specify an 49 } // end class PassArray
This version of class GradeBook uses an 2 // Grade book using an array to store test grades.
3
array of integers to store the grades of 4 public class GradeBook
several students on a single exam. 5 {
6 private String courseName; // name of course this GradeBook represents
This eliminates the need to repeatedly 7 private int grades[]; // array of student grades
input the same set of grades. 8
9 // two-argument constructor initializes courseName and grades array
10 public GradeBook( String name, int gradesArray[] )
11 {
12 courseName = name; // initialize courseName
13 grades = gradesArray; // store grades
14 } // end two-argument GradeBook constructor
15
16 // method to set the course name
17 public void setCourseName( String name )
18 {
19 courseName = name; // store the course name
20 } // end method setCourseName
21
22 // method to retrieve the course name
23 public String getCourseName()
24 {
25 return courseName;
26 } // end method getCourseName
27
02/05/2019 DIANA HAIDAR 18
Case Study: Class GradeBook using an Array to Store Grades
28 // display a welcome message to the GradeBook user
29 public void displayMessage()
30 {
31 // getCourseName gets the name of the course
32 System.out.printf( "Welcome to the grade book for\n%s!\n\n",
33 getCourseName() );
34 } // end method displayMessage
35
36 // perform various operations on the data
37 public void processGrades()
38 {
39 // output grades array
40 outputGrades();
41
42 // call method getAverage to calculate the average grade
43 System.out.printf( "\nClass average is %.2f\n", getAverage() );
44
45 // call methods getMinimum and getMaximum
46 System.out.printf( "Lowest grade is %d\nHighest grade is %d\n\n",
47 getMinimum(), getMaximum() );
48
49 // call outputBarChart to print grade distribution chart
50 outputBarChart();
51 } // end method processGrades
52
53 // find minimum grade
54 public int getMinimum()
55 {
56 int lowGrade = grades[ 0 ]; // assume grades[ 0 ] is smallest
57
02/05/2019 DIANA HAIDAR 19
Case Study: Class GradeBook using an Array to Store Grades
85 // determine average grade for test
86 public double getAverage()
87 {
88 int total = 0; // initialize total
89
90 // sum grades for one student
91 for ( int grade : grades )
92 total += grade;
93
94 // return average of grades
95 return (double) total / grades.length;
96 } // end method getAverage
97
98 // output bar chart displaying grade distribution
99 public void outputBarChart()
100 {
101 System.out.println( "Grade distribution:" );
102
103 // stores frequency of grades in each range of 10 grades
104 int frequency[] = new int[ 11 ];
105
106 // for each grade, increment the appropriate frequency
107 for ( int grade : grades )
108 ++frequency[ grade / 10 ];
109
The compiler counts the number of nested array initializers (represented by set of braces within the outer
braces) in array declaration to determine the number of rows in array.
A multi-dimensional array with the same number of columns in every row can be created with an array-
creation expression.
A multi-dimensional array in which each row has a different number of columns can be created as follows:
30-39:
40-49:
50-59:
60-69: ***
70-79: ******
80-89: ***********
90-99: *******
100: ***
Common programming error: Placing an ellipsis in the middle of a method parameter list is a syntax error.
An ellipsis may be placed only at the end of the parameter list.