Lecture Notes on Arrays - Introduction to Programming in Java | CSIS 110, Study notes of Javascript programming

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

Pre 2010

Uploaded on 08/05/2009

koofers-user-ztv
koofers-user-ztv ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 โ€“ Lecture 32
Quiz Results
mean 13.5
stdev 5.9
median 14
max 22.5
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:
<ElementType>[] <identifier>;
<ElementType>[] <identifier> =
new <ElementType>[ <integer expression> ];
Two forms:
- the first form declares the array variable, but does not allocate the array elements
- The second form declares the array variable, and allocates space for the array
elements - uses the operator 'new' as if we were creating a new object.
Examples
int[] studentID;
int[] quizScores = new int[ 13 ];
boolean[] passingCourse = new boolean[13];
double[] temperature = new double[ 365 ];
int daysPerMonth = new int[ 12 ];
String[] monthNames = new String[12];
pf3
pf4
pf5

Partial preview of the text

Download Lecture Notes on Arrays - Introduction to Programming in Java | CSIS 110 and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 โ€“ Lecture 32

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: [] ; [] = new [ ]; Two forms:

  • the first form declares the array variable, but does not allocate the array elements
  • The second form declares the array variable, and allocates space for the array elements - uses the operator 'new' as if we were creating a new object. Examples int[] studentID; int[] quizScores = new int[ 13 ]; boolean[] passingCourse = new boolean[13]; double[] temperature = new double[ 365 ]; int daysPerMonth = new int[ 12 ]; String[] monthNames = new String[12];

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 '. So the type of temperature is 'array of int'; they type of polygon is 'array of Point'. This means that you can't assign a non-array type to an array. quizScores = 59; // Not legal - different types But I can assign one 'array of x' to another 'array of x'. Indexing The value of an array subscript must be an integer type with a value between 0 and the array-size - 1. If you attempt to access an array element with a subscript outside of this range, Java will throw an IndexOutOfBoundsException, and your program will terminate. daysPerMonth[12] = 31; // Error quizScore[ -1 ] = 40; // Error *** See Array3.java *** Explicit Initialization Remember, after you allocate an array, its elements are initialized to 0, false, or null. These are probably not the values that you want. For some arrays, you know the desired initial values: int[] daysPerMonth = new int[ 12 ]; daysPerMonth[0] = 31; daysPerMonth[1] = 28; ... daysPerMonth[11] = 31;

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 ***