Java Object Oriented Programming Arrays Arrays Arrays are
Java Object Oriented Programming Arrays
Arrays • Arrays are objects that help us organize large amounts of information • Lab 10 focuses on: § array declaration and use § bounds checking and capacity § arrays that store object references Lab 10 -2
Arrays • An array is an ordered list of values Each value has a numeric index The entire array has a single name 0 scores 1 2 3 4 5 6 7 8 9 79 87 94 82 67 98 87 81 74 91 scores. length 10 An array of size N is indexed from zero to N-1 This array holds 10 values that are indexed from 0 to 9 Every array has a length field. It is referenced using the array name. Lab 10 -3
Declaring Arrays Declares the array Dimensions the array int[ ] scores = new int[10]; 3 E 2 FA 5 scores null 0 0 The length of an array is fixed at the time of its creation. Memory Address 0 0 0 0 Lab 10 -4
Declaring Arrays int[ ] scores = new int[10]; 3 E 2 FA 5 scores null 0 0 0 Memory Address 0 0 0 0 Lab 10 -5
Declaring Arrays int[ ] scores = new int[10]; 3 E 2 FA 5 scores 3 E 2 FA 5 0 0 0 Memory Address 0 0 0 0 Lab 10 -6
Declaring Arrays int[ ] scores = new int[10]; 3 E 2 FA 5 scores 3 E 2 FA 5 0 0 0 0 0 Lab 10 -7
Declaring Arrays • Some other examples of array declarations: float[] prices = new float[500]; double[] values; values = new double[20]; Arrays can be be declared and as null dimensioned at and dimensioned the same time. later. Lab 10 -8
Using Arrays • A particular value in an array is referenced using the array name followed by the index in brackets scores[0] = 79; scores[1] = 87; scores[2] = 94; scores[3] = scores[0] + scores[1]; 79 0 0 87 0 0 94 1 0 166 0 3 0 0 5 0 0 7 0 9 2 4 6 8 Lab 10 -9
Bounds Checking • If the array scores can hold 10 values, then it can be indexed using only the numbers 0 to 9 • The following reference will cause an exception to Array. Index. Out. Of. Bounds. Exception be thrown: System. out. println(scores[10]); • It’s common to introduce off-by-one errors when using arrays problem for (int index=0; index <= scores. length; index++) scores[index] = <some value>; Lab 10 -10
Alternate Array Syntax • The brackets of the array type can be associated with the element type or with the name of the array • Therefore the following two declarations are equivalent: float[] prices; float prices[]; • The first format generally is more readable and should be used Lab 10 -11
Initializer Lists • An initializer list can be used to instantiate and fill an array in one step • The values are delimited by braces and separated by commas The new operator • Examples: is not used. int[] units = {147, 323, 89, 933, 540, 269, 97, 114, 298, 476}; char[] letter. Grades = {'A', 'B', 'C', 'D', ’F'}; String[] verbs = {"play", "work", "eat", "sleep"}; Lab 10 -12
Parallel Arrays • Parallel arrays are two or more arrays containing information related/linked by subscript. The data types do not have to be the same. name Joe 18 15 22 Dave Mary 16 14 Don Bob 18 16 Lisa Jim 21 14 Teri 11 Bill Sue age Lab 10 -13
Arrays of Objects The elements of an array can be object references: String[ ] words = new String[5]; 3 E 2 FA 5 words 3 E 2 FA 5 null Memory Address Every element of the array is null Lab 10 -14
Using Arrays of Objects • Creating and storing String objects in the array: words[0] = “cat”; words[1] = “hat”; words[2] = “rat”; words[0]. length() words[3]. length() null cat null hat null rat null 0 1 2 3 4 3 Null. Pointer. Exception Lab 10 -15
The Arrays Class • Included in the java. util package. • Contains various static methods for sorting and searching arrays, comparing arrays, and filling array elements. These methods are overloaded for all primitive types. Lab 10 -16
The Arrays Class Binary Search static int binary. Search(data. Type[] a, data. Type key) Searches the specified array of data. Type for the specified value using the binary search algorithm. static int binary. Search(data. Type[] a, int from. Index, int to. Index, data. Type key) Searches a range of the specified array of data. Type for the specified value using the binary search algorithm. Arrays. binary. Search returns the index of the search key, if it is contained in the array; otherwise, it returns a negative number. Lab 10 -17
The Arrays Class Sort static void sort(data. Type[] a) Sorts the specified array of data. Type into ascending numerical order. static void sort(data. Type[] a, int from. Index, int to. Index) Sorts the specified range of the specified array of data. Type into ascending numerical order. Sorting refers to rearranging all the items in the array into increasing or decreasing order. Lab 10 -18
Java Object Oriented Programming Top Down Design
Top-Down Design (cont’d) • Create a new file called “Lab 10. java”. • Save the new file in your Lab 10 folder. Lab 10 -20
Top-Down Design (cont’d) • Write a program that reads an unknown number of disco songs, the artists that sang them, and the year the songs were released. Store the songs, artists, and release years in a set of parallel arrays. • Display all the songs, artists, and release years. Then sort the array of artists and display them. Lab 10 -21
Top-Down Design (cont’d) import java. util. *; import java. io. *; public class Lab 10 { public static void main(String[ ] args) { Lab 10 lab = new Lab 10( ); lab. input(); // Read from datafile lab. output( ); // Display output } } Lab 10 -22
Top-Down Design (cont’d) We need four instance fields to solve the problem. Lab 10 -23
Top-Down Design (cont’d) public class Lab 10 { private String[] music = new String[50]; private String[] artists = new String[50]; private int[] year = new int[50]; private int count = 0; public static void main(String[ ] args) { Lab 10 lab = new Lab 10( ); lab. input(); // Enter data from the kybd lab. output( ); // Display output } } Lab 10 -24
Top-Down Design (cont’d) input() and output() are the smaller parts of the problem. Lab 10 -25
Top-Down Design (cont’d) public void input() { } public void output() { } Lab 10 -26
Top-Down Design (cont’d) input( ) Lab 10 -27
Top-Down Design (cont’d) public void input() { } Lab 10 -28
Top-Down Design (cont’d) public void input() { try { } catch (IOException e) { } } Lab 10 -29
Top-Down Design (cont’d) public void input() { try { } catch (IOException e) { System. out. println(e); System. exit(0); } } Lab 10 -30
Top-Down Design (cont’d) try { Scanner reader = new Scanner(new File(“lab 10. dat”)); } Lab 10 -31
Top-Down Design (cont’d) try { Scanner reader = new Scanner(new File(“Lab 10. dat”)); reader. use. Delimiter(“/|rn”); } Lab 10 -32
Top-Down Design (cont’d) try { Scanner reader = new Scanner(new File(“Lab 10. dat”)); reader. use. Delimiter(“/|rn”); while (reader. has. Next()) { } } Lab 10 -33
Top-Down Design (cont’d) try { Scanner reader = new Scanner(new File(“Lab 10. dat”)); reader. use. Delimiter(“/|rn”); while (reader. has. Next()) { music[count] = reader. next(); } } Lab 10 -34
Top-Down Design (cont’d) try { Scanner reader = new Scanner(new File(“Lab 10. dat”)); reader. use. Delimiter(“/|rn”); while (reader. has. Next()) { music[count] = reader. next(); artists[count] = reader. next(); } } Lab 10 -35
Top-Down Design (cont’d) try { Scanner reader = new Scanner(new File(“Lab 10. dat”)); reader. use. Delimiter(“/|rn”); while (reader. has. Next()) { music[count] = reader. next(); artists[count] = reader. next(); year[count++] = reader. next. Int(); } } Lab 10 -36
Top-Down Design (cont’d) output( ) Lab 10 -37
Top-Down Design (cont’d) public void output() { } Lab 10 -38
Top-Down Design (cont’d) public void output() { System. out. println(“ Disco Songs”); System. out. println("-----------------"); } Lab 10 -39
Top-Down Design (cont’d) public void output() { System. out. println(“ Disco Songs”); System. out. println("-----------------"); for (int i = 0; i < count; i++) { System. out. println(music[i] + “ by ” + artists[i] + “ (” + year[i] + “)”); } } Lab 10 -40
Top-Down Design (cont’d) public void output() { System. out. println(“ Disco Songs”); System. out. println("-----------------"); for (int i = 0; i < count; i++) { System. out. println(music[i] + “ by ” + artists[i] + “ (” + year[i] + “)”); } System. out. println(); } Lab 10 -41
Top-Down Design (cont’d) public void output() { System. out. println(“ Disco Songs”); System. out. println("-----------------"); for (int i = 0; i < count; i++) { System. out. println(music[i] + “ by ” + artists[i] + “ (” + year[i] + “)”); } System. out. println(); Arrays. sort(artists, 0, count); } Lab 10 -42
Top-Down Design (cont’d) public void output() { System. out. println(“ Disco Songs”); System. out. println("-----------------"); for (int i = 0; i < count; i++) { System. out. println(music[i] + “ by ” + artists[i] + “ (” + year[i] + “)”); } System. out. println(); Arrays. sort(artists, 0, count); System. out. println(“ Artists”); System. out. println(“-----------------”); } Lab 10 -43
Top-Down Design (cont’d) public void output() { System. out. println(“ Disco Songs”); System. out. println("-----------------"); for (int i = 0; i < count; i++) { System. out. println(music[i] + “ by ” + artists[i] + “ (” + year[i] + “)”); } System. out. println(); Arrays. sort(artists, 0, count); System. out. println(“ Artists”); System. out. println(“-----------------”); for (int i = 0; i < count; i++) { System. out. println(artists[i]); } } Lab 10 -44
Run The Program: Disco Songs ----------------Beat It by Michael Jackson (1982) Celebration by Kiil & The Gang (1980) Dance, Dance by Chic (1977) Dancing Queen by Abba (1976) Disco Duck by Rick Dees and His Cast of Idiots (1976) Funkytown by Lipps, Inc. (1979) Hot Stuff by Donna Summer (1975) How Deep Is Your Love by Bee Gees (1979) I Love The Nightlife by Alicia Bridges (1978) I Will Survive by Gloria Gaynor (1979) Stayin' Alive by Bee Gees (1979) A Taste Of Honey by Sukiyaki (1980) Summer Nights by Olivia Newton-John (1992) What A Feeling by Irene Cara (1983) YNCA by Village People (1978) Artists ----------------Abba Alicia Bridges Bee Gees Chic Donna Summer Gloria Gaynor Irene Cara Kiil & The Gang Lipps, Inc. Michael Jackson Olivia Newton-John Rick Dees and His Cast of Idiots Sukiyaki Village People Lab 10 -45
Java Object Oriented Programming Questions?
Java Object Oriented Programming Begin Lab 10
- Slides: 47