Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy...
-
Upload
winifred-page -
Category
Documents
-
view
255 -
download
0
Transcript of Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy...
![Page 1: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/1.jpg)
Chapter 7Arrays
![Page 2: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/2.jpg)
A 12-element array
![Page 3: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/3.jpg)
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 tooString b[] = new String[ 100 ];
![Page 4: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/4.jpg)
Problem
• Develop a test case that create an array of 10 integers and print out the initial values of this array’s elements
![Page 5: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/5.jpg)
Solution 1: Using the Conventional for Loop
![Page 6: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/6.jpg)
Solution 2: Using the for-each Loop
![Page 7: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/7.jpg)
Using an Array Initializer
int n[] = { 10, 20, 30, 40, 50 };– Creates a five-element array– Index values of 0, 1, 2, 3, 4
![Page 8: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/8.jpg)
Problem
• Develop a test case that create an array of the following 10 integers
32, 27, 64, 18, 95, 14, 90, 70, 60, 37
• Print out the values of this array’s elements
![Page 9: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/9.jpg)
Solution 1: Using the Conventional for Loop
![Page 10: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/10.jpg)
Exercise
• Use the for-each loop to rewrite the previous test case
![Page 11: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/11.jpg)
![Page 12: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/12.jpg)
Problem
• Develop a test case that create an array of 10 even integers, starting from 2
• Print out the values of this array’s elements
![Page 13: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/13.jpg)
![Page 14: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/14.jpg)
Problem
• Develop a test case that create an array of the following 10 integers
87, 68, 94, 100, 83, 78, 85, 91, 76, 87
• Calculate the sum of all elements in the array
![Page 15: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/15.jpg)
Solution 1
![Page 16: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/16.jpg)
![Page 17: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/17.jpg)
Question
• Can we make the sum() method to be the static one?
![Page 18: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/18.jpg)
Solution 2: Using varargs
![Page 19: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/19.jpg)
![Page 20: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/20.jpg)
Problem
• Using bar charts to display array data (grade distribution) graphically
• Bar labels: “00-09”, “10-19”, …, “90-99”, and “100”
• See an example in the next slide
![Page 21: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/21.jpg)
Example
• Input: Grade distribution data:
0, 0, 0, 0, 0, 0, 1, 2, 4, 2, 1
• Output: The bar chart
![Page 22: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/22.jpg)
![Page 23: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/23.jpg)
![Page 24: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/24.jpg)
Exercise• Compare the following solution with the previous
one 1 // Fig. 7.6: BarChart.java
2 // Bar chart printing program.
3
4 public class BarChart
5 {
6 public static void main( String args[] )
7 {
8 int array[] = { 0, 0, 0, 0, 0, 0, 1, 2, 4, 2, 1 };
9
10 System.out.println( "Grade distribution:" );
11
12 // for each array element, output a bar of the chart
13 for ( int counter = 0; counter < array.length; counter++ )
14 {
15 // output bar label ( "00-09: ", ..., "90-99: ", "100: " )
16 if ( counter == 10 )
17 System.out.printf( "%5d: ", 100 );
18 else
19 System.out.printf( "%02d-%02d: ",
20 counter * 10, counter * 10 + 9 );
21
22 // print bar of asterisks
23 for ( int stars = 0; stars < array[ counter ]; stars++ )
24 System.out.print( "*" );
25
26 System.out.println(); // start a new line of output
27 } // end outer for
28 } // end main
29 } // end class BarChart
![Page 25: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/25.jpg)
Using the Elements of an Array as Counters
![Page 26: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/26.jpg)
Revisiting the Randomness Verification Problem
• Problem statement: To show that the numbers produced by nextInt() occur with approximately equal likelihood, develop an object that rolls 6000 times of a die. Thus, each integer from 1 to 6 should appear approximately 1000 times
![Page 27: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/27.jpg)
![Page 28: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/28.jpg)
Solution 1: Without Using Array
![Page 29: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/29.jpg)
Solution 2: Using Arrays
![Page 30: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/30.jpg)
![Page 31: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/31.jpg)
![Page 32: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/32.jpg)
Question
• Do a comparison between the Solution 1 and Solution 2
![Page 33: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/33.jpg)
Using Arrays to Analyze Survey Results
![Page 34: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/34.jpg)
Problem
• Develop a method that receives an array of responses about the rating of food quality and returns an array of counts corresponding to each rating
• 1-10 Rating scale: 1 mean awful, 10 means excellent
• See an example in the next slide
![Page 35: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/35.jpg)
Example
• Input:
1, 2, 6, 4, 8, 5, 9, 7, 8, 10,
1, 6, 3, 8, 6, 10, 3, 8, 2, 7,
6, 5, 7, 6, 8, 6, 7, 5, 6, 6,
5, 6, 7, 5, 6, 4, 8, 6, 8, 10
• Expected output:
2, 2, 2, 2, 5, 11, 5, 7, 1, 3
![Page 36: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/36.jpg)
![Page 37: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/37.jpg)
![Page 38: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/38.jpg)
Case Study: Card Shuffling and Dealing Simulation
![Page 39: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/39.jpg)
Sub-Problem 1
• A card has a face and a suit– A face can be one of the following 13 values: Ace,
Deuce, Three, Four, Five,
Six, Seven, Eight, Nine, Ten,
Jack, Queen, King– A suit can be one of the following 4 values: Hearts,
Diamonds, Clubs, and Spades
1. Develop enums for Face and Suit
2. Develop a Card class with the toString() method
![Page 40: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/40.jpg)
![Page 41: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/41.jpg)
![Page 42: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/42.jpg)
![Page 43: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/43.jpg)
![Page 44: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/44.jpg)
Sub-Problem 2
• Develop the DeckOfCards class by making the following test pass
![Page 45: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/45.jpg)
![Page 46: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/46.jpg)
![Page 47: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/47.jpg)
Sub-Problem 3
• Develop a method that can shuffle a deck of cards by making the following test pass
![Page 48: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/48.jpg)
![Page 49: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/49.jpg)
Sub-Problem 4• Develop a method that can deal one card
– Throw an NoSuchElementException if there is no card left
![Page 50: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/50.jpg)
![Page 51: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/51.jpg)
![Page 52: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/52.jpg)
Case Study: Class GradeBook Using an Array/List to Store
Grades
![Page 53: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/53.jpg)
Problem Statement
• A grade book has a course name and a list of grades
• Develop a grade book class that can perform the following tasks1. Constructing a grade book object with a given
course name and a given array of grades
2. Returning the minimum grade
3. Returning the maximum grade
4. Returning the average grade
5. Drawing the bar chart of grades
![Page 54: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/54.jpg)
Task 1: Constructor
![Page 55: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/55.jpg)
Example
• Course name:CS101 Introduction to Java Programming
• Grades:87, 68, 94, 100, 83, 78, 85, 91, 76, 87
![Page 56: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/56.jpg)
![Page 57: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/57.jpg)
![Page 58: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/58.jpg)
Task 2: Minimum Grade
![Page 59: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/59.jpg)
![Page 60: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/60.jpg)
Q: How can we use Collections.min(<aList>)?A: Use java.util.List instead of array
![Page 61: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/61.jpg)
![Page 62: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/62.jpg)
![Page 63: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/63.jpg)
Task 3: Maximum Grade
![Page 64: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/64.jpg)
![Page 65: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/65.jpg)
![Page 66: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/66.jpg)
Task 4: Average Grade
![Page 67: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/67.jpg)
Example
• Input: 87, 68, 94, 100, 83, 78, 85, 91, 76, 87
• Output: 84.9
![Page 68: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/68.jpg)
![Page 69: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/69.jpg)
![Page 70: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/70.jpg)
Task 5: Bar Chart
![Page 71: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/71.jpg)
Example
• Input: 87, 68, 94, 100, 83, 78, 85, 91, 76, 87
• Output:
![Page 72: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/72.jpg)
![Page 73: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/73.jpg)
![Page 74: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/74.jpg)
Multidimensional Arrays
![Page 75: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/75.jpg)
Two-dimensional array with three rows and four columns
![Page 76: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/76.jpg)
Multidimensional Arrays (Cont.)
• Arrays of one-dimensional array– Declaring two-dimensional array b[2][2]
int b[][] = { { 1, 2 }, { 3, 4 } };– 1 and 2 initialize b[0][0] and b[0][1]– 3 and 4 initialize b[1][0] and b[1][1]
int b[][] = { { 1, 2 }, { 3, 4, 5 } };
– row 0 contains elements 1 and 2– row 1 contains elements 3, 4 and 5
![Page 77: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/77.jpg)
Multidimensional Arrays (Cont.)
• Two-dimensional arrays with rows of different lengths– Lengths of rows in array are not required to be the
same• E.g., int b[][] = { { 1, 2 }, { 3, 4, 5 } };
![Page 78: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/78.jpg)
Multidimensional Arrays (Cont.)
• Creating two-dimensional arrays with array-creation expressions– Can be created dynamically
•3-by-4 array
int b[][]; b = new int[ 3 ][ 4 ];
• Rows can have different number of columns int b[][]; b = new int[ 2 ][ ]; // create 2 rows b[ 0 ] = new int[ 5 ]; // create 5 columns for row 0
b[ 1 ] = new int[ 3 ]; // create 3 columns for row 1
![Page 79: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/79.jpg)
Outline 1 // Fig. 7.17: InitArray.java
2 // Initializing two-dimensional arrays.
3
4 public class InitArray
5 {
6 // create and output two-dimensional arrays
7 public static void main( String args[] )
8 {
9 int array1[][] = { { 1, 2, 3 }, { 4, 5, 6 } };
10 int array2[][] = { { 1, 2 }, { 3 }, { 4, 5, 6 } };
11
12 System.out.println( "Values in array1 by row are" );
13 outputArray( array1 ); // displays array1 by row
14
15 System.out.println( "\nValues in array2 by row are" );
16 outputArray( array2 ); // displays array2 by row
17 } // end main
18
Use nested array initializers to initialize array1
Use nested array initializers of different lengths to
initialize array2
![Page 80: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/80.jpg)
Outline19 // output rows and columns of a two-dimensional array
20 public static void outputArray( int array[][] )
21 {
22 // loop through array's rows
23 for ( int row = 0; row < array.length; row++ )
24 {
25 // loop through columns of current row
26 for ( int column = 0; column < array[ row ].length; column++ )
27 System.out.printf( "%d ", array[ row ][ column ] );
28
29 System.out.println(); // start new line of output
30 } // end outer for
31 } // end method outputArray
32 } // end class InitArray Values in array1 by row are 1 2 3 4 5 6 Values in array2 by row are 1 2 3 4 5 6
array[row].length returns number of columns associated with row subscript
Use double-bracket notation to access two-dimensional array values
![Page 81: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/81.jpg)
7.9 Multidimensional Arrays (Cont.)
• Common multidimensional-array manipulations performed with for statements– Many common array manipulations use for
statements
E.g., for ( int column = 0; column < a[ 2 ].length; column++ ) a[ 2 ][ column ] = 0;
![Page 82: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/82.jpg)
Case Study: Class GradeBook Using a Two-Dimensional Array
![Page 83: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/83.jpg)
Problem Statement
• A grade book has a course name and a list of grades. Each student has more than one grades
• Develop a grade book class that can perform the following tasks1.Constructing a grade book object with a given course
name and a given 2D-array of grades
2.Returning the minimum grade
3.Returning the maximum grade
4.Returning a list of average grades
5.Drawing the bar chart of grades
![Page 84: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/84.jpg)
Examples
• GradesTest1 Test2 Test3
Student 1: 87 96 70Student 2: 68 87 90Student 3: 94 100 90Student 4: 100 81 82Student 5: 83 65 85Student 6: 78 87 65Student 7: 85 75 83Student 8: 91 94 100Student 9: 76 72 84Student 10: 87 93 73
![Page 85: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/85.jpg)
Task 1: Constructor
![Page 86: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/86.jpg)
![Page 87: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/87.jpg)
![Page 88: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/88.jpg)
Task 2: Minimum Grade
![Page 89: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/89.jpg)
![Page 90: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/90.jpg)
![Page 91: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/91.jpg)
Task 3: Maximum Grade
![Page 92: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/92.jpg)
![Page 93: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/93.jpg)
![Page 94: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/94.jpg)
Task 4: Average Grades
![Page 95: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/95.jpg)
![Page 96: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/96.jpg)
![Page 97: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/97.jpg)
Task 4: Drawing Bar Chart
![Page 98: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/98.jpg)
![Page 99: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/99.jpg)
![Page 100: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/100.jpg)
![Page 101: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/101.jpg)
Case Study: The Sudoku Puzzle
![Page 102: Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]](https://reader035.fdocuments.net/reader035/viewer/2022062309/56649ed85503460f94be6c64/html5/thumbnails/102.jpg)
What You Have Learnt?
• To be filled