Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CSIS 110 Lecture 33: Arrays - Prof. Brian F. Hanks, Study notes of Javascript programming

An introduction to arrays in java programming, including their declaration, initialization, accessing elements, and allocation of elements. It also covers the concept of array size and type, as well as common errors and exercises.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-s6c
koofers-user-s6c 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 – Lecture 33
Chapter 8 - Arrays
What is 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];
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
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
pf3
pf4
pf5

Partial preview of the text

Download CSIS 110 Lecture 33: Arrays - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 33

Chapter 8 - Arrays What is 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 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 ); polygon[2] = new Point( 12, 2 ); Accessing array elements In other situations, a subscripted array element is an expression that can be used anywhere that an expression of that type is allowed. System.out.println( "There are " + daysPerMonth[ 3 ] + " in " + monthName[3] ); Point firstPoint = polygon[0]; sum += quizScore[ i ]; if ( passingCourse[ 0 ] ) System.out.println( "Student is passing." ); *** Look at array1.java *** Allocating the array elements Let's look at some of our array declarations again

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 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 *** Random Numbers Let's say that I wanted to generate a sequence of random numbers in the range 1 to 6. How could I do it in the real world (without a computer)?

  • coins
  • dice
  • drawing ping pong balls What are random numbers used for?
  • Games/Lottery
  • Controlled Experiments (Control/Treatment groups)
  • Simulations
  • Cryptography
  • Randomly selecting advertisements to appear on web pages Java has a class named Random that can be used to generate sequences of psuedo-random numbers. import java.util.Random;