130 likes | 207 Views
***** SWTJC STEM *****. What Are Arrays?. An array is a simple but powerful way to organize and store large amounts of data and information in a program.
E N D
***** SWTJC STEM ***** What Are Arrays? • An array is a simple but powerful way to organize and store large amounts of data and information in a program. • Other common data structures are linked lists, queues, stacks, vectors, and hash tables. As CS major, you will take an entire course devoted to data structures. • An array is an orderly arrangement of data that can be single or multiple dimensioned. • A single dimension array consists of a collection of data, all of the same type, with each element of data referred to by a single position index. • The array has a single name and uses a numeric (integer) index to refer to any one of its data values. Chapter 7 cg 68
***** SWTJC STEM ***** Creating An Array • Java utilizes a zero based index, that is, the first data value (element of the array) is indexed at zero (not “one” as you might expect). • The first array value in array myList is myList[0]. • Creating an array is a two-step process. • Declare and instantiate the array using the new operator:int[ ] myList = new int[4]; // 4 data elements (index 0-3) • Initialize the array:for (int i = 0; i < myList.length; i++) myList[i] = i * i; // Array elements are 0, 1,...,3 Chapter 7 cg 68
***** SWTJC STEM ***** One-Dimensional Array myList[ ] one-dimensional array 4 X 1 (4 rows by 1 column) 0 i = 0 1 i = 1 4 i = 2 9 i = 3 Chapter 7 cg 68
***** SWTJC STEM ***** Creating An Array • Note that once the array object (myList) has been instantiated, properties like its size (referred to as length) can be accessed. • Note that if the length is “n”, the last element referenced is “n-1”. For example array myList(n=4), the last data element is myList[3]. • For small arrays, the instantiation process can be simplified using an initializer list:int[ ] myList = {0, 1, 4, 9}; • Note that the property length is determined by the number of elements in the list; in this case myList.length is 4. Chapter 7 cg 69
***** SWTJC STEM ***** 1. Using a for loop (ForLoopArray.java): int[ ] myList1 = new int[5]; for (int i = 0; i < myList1.length; i++) myList1[i] = i * i;for (int i = 0; i < myList1.length; i++) System.out.println(i + “ “ + myList1[i]); 2. Using an initializer list (InitializerArray.java): int[ ] myList1 = {0, 1, 4, 9, 16}; for (int i = 0; i < myList1.length; i++) System.out.println(i + “ “ + myList1[i])i; Creating an Array i = 0 i = 1 i = 2 i = 3 i = 4 Chapter 7 cg 69
***** SWTJC STEM ***** • One advantage of arrays is that you can start with an algorithm coded for a single value and apply it to a data set containing many values. • Consider a payroll calculation program:“Code a program to compute payroll for several employees” • The list of hours worked by each employee will be assigned to a one-dimensional array. • Since we are processing a array of known size, a for loop is a natural selection. • The core of our for loop is an algorithm for pay calculation with the hours worked variable replaced with an indexed array variable. Array Calculations Chapter 7 cg 70
***** SWTJC STEM ***** PayrollArray.java Payroll Example double rate = 10.00, totalPay; double[ ] hours = { 35., 45., 40., 38. }; for (int i = 0; i < hours.length; i++) { if (hours[i] <= 40) { totalPay = hours[i] * rate; } else { totalPay = (hours[i] - 40) * 1.5 * rate + 40.0 * rate; } System.out.println(i + " " + hours[ i ] + " $" + totalPay); } 35. i = 0 45. i = 1 40. i = 2 38. i = 3 Chapter 7 cg 70
***** SWTJC STEM ***** Payroll Example Results 0 35.0 $350.0 1 45.0 $475.0 2 40.0 $400.0 3 38.0 $380.0 Chapter 7 cg 70
***** SWTJC STEM ***** Letter Count Example • Consider a program that counts by letter the number of upper and lower letters in a sentence. • As a sentence is scanned letter by letter, we will accumulate the count for each letter storing it until the entire sentence is scanned. • This means we need 26 times 2 (A-Z and a-z) or 52 variables. We naturally choose arrays to do the job. • We declare two integer arrays, upper[26 ]andlower[26]. • Assumeupper[0]will store the total number of capitalA’s,upper[1]for B’s, etc. Similarly forlowercase. • Finally, variableotherwill accumulate the non-letters. Chapter 7 cg 70
***** SWTJC STEM ***** LetterCount.java Letter Count Example // Declare and input final int NUMCHARS = 26; int[ ] upper = new int[NUMCHARS]; int[ ] lower = new int[NUMCHARS]; char current; // the current character being processed int other = 0; // counter for non-alphabetics String line = JOptionPane.showInputDialog(null, "Enter a sentence:", "Letter Count", JOptionPane.QUESTION_MESSAGE); Chapter 7 cg 70
***** SWTJC STEM ***** LetterCount.java cont. Letter Count Example // Count the number of each letter occurence for (int ch = 0; ch < line.length(); ch++) { current = line.charAt(ch); if (current >= 'A' && current <= 'Z') upper[current -'A']++; // upper[0] is ‘A’ ... upper[25] is ‘Z’ else if (current >= 'a' && current <= 'z') lower[current -'a']++; // lower[0] is ‘a’ ... lower[25] is ‘z’ else other++; } Chapter 7 cg 70
***** SWTJC STEM ***** LetterCount.java cont. Letter Count Example // Print the results System.out.println (); for (int letter=0; letter < upper.length; letter++) { System.out.print ( (char) (letter + 'A') ); System.out.print (": " + upper[letter]); System.out.print ("\t\t" + (char) (letter + 'a') ); System.out.println (": " + lower[letter]); } System.out.println (); System.out.println ("Non-alphabetic characters: " + other); } Chapter 7 cg 70
2 0 1 ***** SWTJC STEM ***** The truck is a Ford Ranger. A: 0 a: 2 B: 0 b: 0 C: 0 c: 1 D: 0 d: 1 E: 0 e: 2 F: 1 f: 0 G: 0 g: 1 H: 0 h: 1 I: 0 i: 1 ......... Y: 0 y: 0 Z: 0 z: 0 Non-alphabetic characters: 6 Letter Count Example lower[letter ] letter = 0 letter = 1 letter = 2 ........ letter = 24 0 letter = 25 0 Chapter 7 cg 70