Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class The project assignment, againThe project assignment, again Arrays.

Similar presentations


Presentation on theme: "March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class The project assignment, againThe project assignment, again Arrays."— Presentation transcript:

1 March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class The project assignment, againThe project assignment, again Arrays - Chapter 8Arrays - Chapter 8 –Declaring, initializing, special ‘for’ statement –Sample with Random –Sizing issues –Using typed-in input

2 March 2005 2/18R. Smith - University of St Thomas - Minnesota Class Projects Something I can ‘run’Something I can ‘run’ Does something visibleDoes something visible –Graphics and/or file processing Class definitions in multiple files (2 or more)Class definitions in multiple files (2 or more) May be solo, or teams of 2 or 3May be solo, or teams of 2 or 3 –One separate file/class per team member Project ideas?Project ideas? –Text-based game –Tax return: maybe just a simple 1040 form –A “complete” payroll implementation Correct taxes, multiple employeesCorrect taxes, multiple employees –House design/costing (extension of the rug problem)

3 March 2005 3/18R. Smith - University of St Thomas - Minnesota Arrays A collection of ‘things’ of the same typeA collection of ‘things’ of the same type –May be numbers (primitive types) –Strings (reference types) –Objects (reference types) Each array has a size, fixed when createdEach array has a size, fixed when created Use brackets: [, and ], to indicate an arrayUse brackets: [, and ], to indicate an array int myArray[] = new int[20];int myArray[] = new int[20]; –An array of 20 integers myArray[10] = 5; // initialize the 10th elementmyArray[10] = 5; // initialize the 10th element

4 March 2005 4/18R. Smith - University of St Thomas - Minnesota Declaring, creating an Array To define: type name, variable name, “[]”To define: type name, variable name, “[]” –You don’t have to initialize it but you can ExamplesExamples –double daysWages[]; –daysWages = new double[7]; – - or - –double daysWages[] = new double[7]; – - or - –final int DAYS = 7; –double daysWages[] = new double[DAYS];

5 March 2005 5/18R. Smith - University of St Thomas - Minnesota Alternate array declarations int[] numbers;int[] numbers; int numbers[];int numbers[]; int[] numbers, codes, scores;int[] numbers, codes, scores; int numbers[], codes[], scores[];int numbers[], codes[], scores[]; What about…What about… int numbers[], codes, scores;int numbers[], codes, scores; Initializing an array:Initializing an array: –int numbers[] = {1, 3, 5, 7, 9};

6 March 2005 6/18R. Smith - University of St Thomas - Minnesota Using an array (an “array reference”) In the array with 7 intsIn the array with 7 ints –Elements are numbered 0 to 6 –First is daysWages[0] –Last is daysWages[6] Treat an array reference like any other variableTreat an array reference like any other variable –You can assign to it –You can use it in arithmetic calculations –You can pass its contents to a method

7 March 2005 7/18R. Smith - University of St Thomas - Minnesota Working through an array int numbers[] = {1, 3, 5, 7, 9};int numbers[] = {1, 3, 5, 7, 9}; int sum = 0;int sum = 0; for (i=0; i<numbers.length; i++)for (i=0; i<numbers.length; i++) –sum = sum + numbers[i]; “Special version” of for statement“Special version” of for statement –for (int val : numbers) sum = sum + val;sum = sum + val;

8 March 2005 8/18R. Smith - University of St Thomas - Minnesota The “special” for statement “Special version” of for statement“Special version” of for statement –for (int val : numbers) sum = sum + val;sum = sum + val; Creates a variable that’s “local” to the loopCreates a variable that’s “local” to the loop –Each time through the loop, the variable gets the value –You can’t change values in the array –You can only “look” at array values

9 March 2005 9/18R. Smith - University of St Thomas - Minnesota An array exercise Use ‘random’ to generate integersUse ‘random’ to generate integers –Fill up a small array using a ‘for’ loop –Import java.util.Random –Method nextInt(num) returns int from 0 to num Use a regular ‘for’ loop to fill up the arrayUse a regular ‘for’ loop to fill up the array Find the ‘min’ or ‘max’ or ‘average’ or ‘sum’Find the ‘min’ or ‘max’ or ‘average’ or ‘sum’ –Use the special ‘for’ loop for this

10 March 2005 10/18R. Smith - University of St Thomas - Minnesota Picking the size If you know it ahead of time, perfectIf you know it ahead of time, perfect If user knows it, ask for itIf user knows it, ask for it –Example: number of days of pay –Double check what the user says, of course If you’re reading a file, you can read it twiceIf you’re reading a file, you can read it twice –First time, count the number of lines, or of whatever –Create an array with the right number of elements –Next, read the file again and copy the data into the array

11 March 2005 11/18R. Smith - University of St Thomas - Minnesota What if you don’t know the size? Pick a ‘worst case’ sizePick a ‘worst case’ size –I.e. bigger that it ever needs to be –Be sure you check for attempted overflow! The.length attributeThe.length attribute –Use it with the array name to get the array size –daysWages.length == 7 What happens to a bad array reference?What happens to a bad array reference? –For example, j = daysWages[8] –It doesn’t work! Java detects it. If it’s a constant, then the compiler finds itIf it’s a constant, then the compiler finds it If it’s calculated during execution, the runtime finds itIf it’s calculated during execution, the runtime finds it

12 March 2005 12/18R. Smith - University of St Thomas - Minnesota User input example Calculate from numbers typed inCalculate from numbers typed in –Use a ‘sentinel’ to mark the end –Check for the sentinel or end of array Once the numbers are in the array, go to workOnce the numbers are in the array, go to work

13 March 2005 13/18R. Smith - University of St Thomas - Minnesota That’s it. Questions?Questions? Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.


Download ppt "March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class The project assignment, againThe project assignment, again Arrays."

Similar presentations


Ads by Google