Building Java Programs Chapter 7 Arrays These lecture

Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, 2006. They may not be rehosted, sold, or modified without expressed permission from the authors. All rights reserved. 1

Chapter outline n Array basics n n n declaring and initializing an array getting and setting values of elements of an array limitations of arrays n Arrays for counting and tallying n Array traversal algorithms n Advanced array usage n n n Shifting elements in an arrays as parameters to methods String methods that use arrays Graphics methods that use arrays the Arrays class command-line arguments 2

Array basics n suggested reading: 7. 1 3

A problem we can't solve (yet) n Consider the following program (input underlined): How Day Day many days' temperatures? 7 1's high temp: 45 2's high temp: 44 3's high temp: 39 4's high temp: 48 5's high temp: 37 6's high temp: 46 7's high temp: 53 Average temp = 44. 57142857 4 days were above average. n We need access to the temperatures once to compute the average, and then again to tell how many were above average. 4

Why the problem is tough n We appear to need each input value twice: n n n once to compute the average a second time to count how many were above average We could examine each value twice if we could read them into variables. However, we don't know in advance how many variables we'll need, because we don't know how many days' weather are coming until the program is running. We need a way to declare many variables' worth of memory in a single step. 5

Another tough problem n Given a file of integer exam scores, such as: 82 66 79 63 83 81 n Write a program that will print a text histogram of stars indicating the number of students who earned each unique exam score. 85: 86: 87: 88: 91: ************ * **** 6

Arrays n array: A single variable that can store many values of the same type. n n n element: One value in an array. index: A 0 -based integer, which is used to access an element from an array. We usually draw an array as a row or column of boxes. n Example: an array of ten integers index 0 1 value 12 49 element 0 2 3 4 5 6 -2 26 5 17 -6 element 4 7 8 84 72 9 3 element 9 7
![Array declaration n Declaring/initializing an array: <type> [] <name> = new <type> [ <length> Array declaration n Declaring/initializing an array: <type> [] <name> = new <type> [ <length>](http://slidetodoc.com/presentation_image_h2/b631e421d9f6c4e9731ed81ef63882f2/image-8.jpg)
Array declaration n Declaring/initializing an array: <type> [] <name> = new <type> [ <length> ]; n n n Example: int[] numbers = new int[10]; The length of the array is specified between [ ] brackets. index 0 1 2 3 4 5 6 7 8 9 value 0 0 0 0 0 The array's length can be any expression, such as a constant or variable's value. n Example: int x = 2 * 3 + 1; int[] numbers = new int[x % 5 + 2]; 8

Array auto-initialization n When arrays are initially constructed, every element is automatically initialized to a "zero-equivalent" value. n n n int: 0 double: 0. 0 boolean: false char: '