



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Hanks; Class: Intro to Programming in Java; Subject: Computer Science Info Systems; University: Fort Lewis College; Term: Winter 2005;
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Quiz Results mean 13. stdev 5. median 14 max 22. Distribution: <10: 4, 10-15: 4 15.5 โ 20: 4 20+: 1 Chapter 8 - Arrays Let's consider the quiz scores that we just discussed. Suppose I wanted to write a Java program that kept track of the quiz scores. What variables would I need? Now, suppose I wanted to determine the minimum and maximum scores from these variables. What if I was teaching at a school where there were 100 students in a class? So, this is very awkward, and not very flexible. I'd like to be able to have a general mechanism for keeping a set of values in a list. Java provides a mechanism for this - it is called an array. An array is a set of variables of the same type that have a single identifier associated with them. Each of the variables is called an element of the array. Declaration of an array variable:
Point[] polygon; Initialization Draw pictures of above. Show that an array is a reference type. The array identifier refers to the array reference. For those arrays that are initialized, show that numeric types are initialized to 0, booleans to false, and reference types to null. Emphasize that declaring the array only creates the reference. You have to allocate the array elements also! Accessing array elements Ok, this is all great. I've created these arrays that are sets of variables with the same name. How do I actually use the array elements in a program? We access the array elements using subscripting or indexing. Each array element has a subscript (or index ) that we can use to refer to that element. The subscripts are integer values, starting at 0. Thus, the first element of the array has subscript 0, the second has subscript 1, and the last has a subscript of the array-size - 1. int[] daysPerMonth = new int[12]; Draw picture, showing the array subscripts, and the names of each element (daysPerMonth[0], daysPerMonth[1], ...) The elements of an array are referenced using the array name and a subscript. So, we can say daysPerMonth[6] or quizScores[4] or temperature[300]. The value of the index or subscript MUST BE an integer expression. That means we don't have to use constants. temperature[ i ] quizScore[ last / 2 ] Assigning values to array elements Use an assignment statement, but the variable on the lhs is indexed. monthName[0] = "January"; daysPerMonth[0] = 31; // January (1 - 1) daysPerMonth[10] = 30; // November (11 - 1) quizScores[0] = 15; polygon[0] = new Point( 3, 5 ); polygon[1] = new Point( 7, 10 );
*** Look at Array2.java *** Note that length is not a method - there are no () here. So, arrays - arrayName.length quizScore.length Strings - stringName.length() String s = "hello"; s.length() Type of an array What is the type of the array temperature? How about polygon? The type of an array is 'array of
This is tedious. Java provides a nice shorthand for this: int[] daysPerMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; *** See Array4.java *** for loops and arrays For loops are frequently used to access all of the elements of an array - Look at ArrayLoop1.java and ArrayLoop2.java. int[] anArray = { 15, 9, 12 }; int sum = 0; for ( int i = 0; i < anArray.length; i++ ) { sum += anArray[i]; } System.out.println("The sum of the array elements is " + sum ); Remember the difficulty we would have finding the smallest of a set of variables? It is much easier with arrays - how would we do it? Another example (see ArrayLoop3.java): double[] array2 = { 5.6, 7.2, 3.9, 8.4, 12.1 }; double min = array2[0]; for ( int i = 0; i < array2.length; i++ ) { if ( array2[i] < min ) min = array2[i]; } Common errors - incorrect indexing! // Skips first element of array - // not always wrong, but usually is for ( int i = 1; i < array2.length; i++ ) { ... } // Goes past last element of array - always wrong for ( int i=0; i <= array2.length; i++ ) { ... } *** See ArrayLoop4.java ***