



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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 6
This page cannot be seen from the preview
Don't miss anything!
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:
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)?