Download presentation
Presentation is loading. Please wait.
Published byHomer Owens Modified over 8 years ago
1
13 Arrays CE00858-1: Fundamental Programming Techniques June 161
2
13 Arrays Objectives In this lecture, we will introduce the concept of an array introduce the syntax of arrays and the keyword new discuss array initialization write some simple examples using arrays June 162
3
13 Arrays Single values variables are used to hold data about a single thing: what if we want to store data about lots of items? number of days in each month 20 mark June 163
4
13 Arrays Many values of same type one declaration for each month code would need to be repeated 12 times program length would be unmanageable implementation would be extremely error-prone testing would take vast amounts of time debugging would be highly time-consuming 3128313031 30 31 30313031 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec June 164
5
13 Arrays Arrays group similar items into a single logical unit consecutive memory locations each holds data of the same type array has a single name upper and lower bounds indicate size each element is identified using array name and index 01234567891011 monthDaysArray name Lower boundArray indexmonthDays (8)Upper bound June 165
6
13 Arrays Array bounds bounds of an array refer to first and last element that can be indexed first item: 0 last item: length – 1 index is checked against the bounds of the array if index is out of range an exception is thrown ArrayIndexOutOfBoundsException if not handled the program will terminate June 166
7
13 Arrays Array declaration to declare an array, the programmer specifies: the type of data to be stored a name that refers to the whole structure the size of the array June 167 int [ ] monthDays = new int [12]; 0 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 monthDays
8
13 Arrays Array declaration and initialisation June 168 int [ ] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31 0 28 1 31 2 30 3 31 4 30 5 31 6 7 30 8 31 9 30 10 31 11 monthDays array can also be initialised when it is created: the type of data to be stored a name that refers to the whole structure the values to be stored
9
13 Arrays String arrays String [] monthName = { "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"}; June 169 Jan 0 Feb 1 Mar 2 Apr 3 May 4 June 5 July 6 Aug 7 Sep 8 Oct 9 Nov 10 Dec 11 monthName arrays can be any type, as long as all elements are the same type:
10
13 Arrays Accessing individual elements each individual element is uniquely identified by: array name index or position, enclosed by [] first element is in position 0 each element is of type indicated in declaration June 1610 monthDays[1] = monthDays[1] + 1; 31 0 29 1 31 2 30 3 31 4 30 5 31 6 7 30 8 31 9 30 10 31 11 monthDays
11
13 Arrays Accessing elements example – DaysInMonth problem: output number of days in month input by user June 1611 analysis: what data is used? monthNum: integer input by user monthDays: integer array set to number of days in each month monthNames: string array set to names of each month what operations are performed? create Scanner initialise monthDay array to number of days in each month initialise monthName array to names of months input monthNum subtract 1 from monthNum to get value in range 0 – 11 output number of days in monthNum location of monthDays
12
13 Arrays DaysInMonth code Scanner kybd = new Scanner (System.in); int [] monthDay = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; String [] monthName = { "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"}; System.out.print("Which month?"); int monthNum = kybd.nextInt(); monthNum = monthNum – 1; System.out.println("There are " + monthDay[monthNum] + " days in " + monthName[monthNum]); June 1612 DaysInMonth.java
13
13 Arrays Accessing all elements for loop can be used to step through the items can perform same operation on each location in an array value of loop counter used as index need to start at location 0 and go up to array length array has property called length that holds the length of the array accessed using array name followed by.length June 1613
14
13 Arrays Accessing all elements – DaysInAllMonths problem: output number of days in each month in turn June 1614 analysis: what data is used? monthDays: integer array set to number of days in each month monthNames: string array set to names of each month what operations are performed? iteration needed as days in each month need to be output what operations are done once before the loop? initialise monthDay array to number of days in each month initialise monthName array to names of months how many times is loop repeated? loop repeated for each month, month = 0 to length of array what operations are repeated inside loop? output number of days in month what operations are done once after the loop? none
15
13 Arrays DaysInAllMonths code int [] monthDay = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; String [] monthName = { "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"}; for (int i = 0; i < monthDay.length; i++) { System.out.println("There are " + monthDay[i] + " days in " + monthName[i]); } June 1615 DaysInAllMonths.java
16
13 Arrays Reading data into an array - Largest data can be read into an array in a similar way to reading into a variable June 1616 problem: read 10 integers and store them in an array find and output the largest value in the array and its location
17
13 Arrays Largest analysis analysis : what data is used? numbers: integer array used to store 10 integers input by user largest: integer to hold largest value calculated by program largestLocation: integer to hold the location of the largest value calculated by program what constructs are needed? iteration to read data in second iteration to find largest element and its location selection inside second iteration as largest value needs to be stored what operations are done once before loop to read in data? create Scanner create array to hold 10 integers how many times is loop to read in data repeated? loop repeated 10 times, i = 0 to 9 what operations are repeated inside loop to read in data? read number into next array location June 1617
18
13 Arrays Largest analysis what operations are done once after loop to read in data? store contents of first array location in largest store position of first array location (ie. 0) in largestLocation how many times is loop to find largest repeated? loop repeated 9 times (do not need to check first location), i = 1 to 9 what operations are repeated inside loop to find largest? check the contents of the array location against largest what operations are done if the contents of the array location are greater than largest? store contents of array location in largest store position of array location (ie. i) in largestLocation what operations are done once after loop to find largest? output largest output largestLocation June 1618
19
13 Arrays Largest code Scanner kybd = new Scanner(System.in); //create array to hold 10 integers int [ ] numbers = new int [10]; System.out.println("Enter 10 integers: " ); //read 10 integers into array for (int i = 0; i < numbers.length; i++) { numbers[i] = kybd.nextInt(); } //initialise largest value and location to first array element int largest = numbers[0]; int largestLocation = 0; June 1619 Continued on next slide Largest.java
20
13 Arrays //step through each array location for (int i = 1; i < numbers.length; i++) { //check if array location is new largest if (numbers[i] > largest) { largest = numbers[i]; largestLocation = i; } //output value and location of largest element System.out.println("Largest value: " + largest); System.out.println("Location of largest value: " + largestLocation); June 1620
21
13 Arrays Summary In this session we have: discussed arrays covered the difference between an array location and its contents seen how arrays are declared and initialised in Java implemented examples that store data in arrays In the next session we will: analyse and implement a program that uses arrays June 1621
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.