Presentation is loading. Please wait.

Presentation is loading. Please wait.

***** SWTJC STEM ***** Chapter 7 cg 68 What Are Arrays? An array is a simple but powerful way to organize and store large amounts of data and information.

Similar presentations


Presentation on theme: "***** SWTJC STEM ***** Chapter 7 cg 68 What Are Arrays? An array is a simple but powerful way to organize and store large amounts of data and information."— Presentation transcript:

1 ***** SWTJC STEM ***** Chapter 7 cg 68 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.

2 ***** SWTJC STEM ***** Chapter 7 cg 68 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. 1.Declare and instantiate the array using the new operator: int[ ] myList = new int[4]; // 4 data elements (index 0-3) 2.Initialize the array: for (int i = 0; i < myList.length; i++) myList[i] = i * i; // Array elements are 0, 1,...,3

3 ***** SWTJC STEM ***** Chapter 7 cg 68 One-Dimensional Array 9 4 1 0 i = 0 i = 1 i = 2 i = 3 myList[ ] one-dimensional array 4 X 1 (4 rows by 1 column)

4 ***** SWTJC STEM ***** Chapter 7 cg 69 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.

5 ***** SWTJC STEM ***** Chapter 7 cg 69 Creating an Array 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; 0 1 4 9 16 i = 0 i = 4 i = 1 i = 2 i = 3

6 ***** SWTJC STEM ***** Chapter 7 cg 70 Array Calculations 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.

7 ***** SWTJC STEM ***** Chapter 7 cg 70 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); } 38. 40. 45. 35. i = 0 i = 1 i = 2 i = 3 PayrollArray.java

8 ***** SWTJC STEM ***** Chapter 7 cg 70 Payroll Example Results 0 35.0 $350.0 1 45.0 $475.0 2 40.0 $400.0 3 38.0 $380.0

9 ***** SWTJC STEM ***** Chapter 7 cg 70 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 ] and lower[26 ]. Assume upper[0] will store the total number of capital A’s, upper[1] for B’s, etc. Similarly for lower case. Finally, variable other will accumulate the non-letters.

10 ***** SWTJC STEM ***** Chapter 7 cg 70 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); LetterCount.java

11 ***** SWTJC STEM ***** Chapter 7 cg 70 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++; } LetterCount.java cont.

12 ***** SWTJC STEM ***** Chapter 7 cg 70 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); } LetterCount.java cont.

13 ***** SWTJC STEM ***** Chapter 7 cg 70 Letter Count Example The truck is a Ford Ranger. A: 0a: 2 B: 0b: 0 C: 0c: 1 D: 0d: 1 E: 0e: 2 F: 1f: 0 G: 0g: 1 H: 0h: 1 I: 0i: 1......... Y: 0y: 0 Z: 0z: 0 Non-alphabetic characters: 6 1 0 2 letter = 0 letter = 25 letter = 1 letter = 2 letter = 24 lower[letter ] 0 0........


Download ppt "***** SWTJC STEM ***** Chapter 7 cg 68 What Are Arrays? An array is a simple but powerful way to organize and store large amounts of data and information."

Similar presentations


Ads by Google