Download presentation
Presentation is loading. Please wait.
Published byDrusilla McKenzie Modified over 9 years ago
2
More Arrays Length, constants, and arrays of arrays By Greg Butler
3
Overview/Purpose In the first lesson we learned that an array is a variable that can hold multiple values of the same data type. We also explored the definition, creation and initialization of a single dimensional array to hold primitive data types. In so doing we learned how to declare the array variable and using either the new operator or a list of values. We then looked at a simple program that allows us to retrieve and use the values stored in the array’s elements. This lesson introduces us to the use the length variable to retrieve the size of an array and use a constant with the new operator to specify array size. We then look into the use of “multiple dimensional” arrays. The next lesson will then look into sorting and searching arrays.
4
Objectives The student when presented with a short answer question, matching question, or programming problem, must demonstrate: –An understanding of the meaning and use of the “length” instance variable to find the size of an array. –The use of the “final” keyword to create a named constant. –An understanding of the meaning and use of constants to specify array size and to make software changes easier to make. –The ability to describe, create, and populate an array of arrays using both the “new” operator and by populating the elements at the time the array is defined. –The ability to access the elements of a multi-dimensional arrays so that they can be update and used in a program.
5
Finding the Length of an Array Array are actually objects of a particular class. We will explore this in detail later, but for now, it is sufficient to know that the length of an array is stored inside a variable* called length. Usage: my_array_name. length Specify the name of your array followed by the dot operator (“.”) and the term “length” and it retrieves the size of the named array. In this next example we modify our bowlingScores program to use the array length method to determine the maximum value in the for loop, instead of specifying the maximum value explicitly. * In OO terms, it is an instance variable, because every array you create has its own value for “length”.
6
Finding the Length of an Array Example class ArrayBasicsFindLength{ public static void main(String args[]){ int bowlingScores[] = {117, 127, 110, 85, 135}; int index = 0; // declare and initialize a variable to hold the // current array index number for (index = 0; index < bowlingScores.length; index++) { // display the results System.out.println (“ Element “+ index + “ added to 5 is: “ + outputValue); } // end for } //end main } //end class ArrayBasicsOne Since the array is of size 5, bowlingScores.length is 5.
7
Using Constants (final variables) to Specify Array Size Constants are variables whose value cannot be changed by the program once it is assigned. “final” is the reserve word that designates a constant. By convention, constant names appear in upper case. The use of constants helps make a program more maintainable; you only need to make the change one place in the program (where the value is initially assigned to a constant) and it is applied everyplace the constant appears In our example, we create and initialize an array and then read out the elements using a constant to designate the size of the array.
8
import javax.swing.*; public class ArrayBasicsConstants{ public static void main(String args[]){ final int CONST_DEMO_SIZE = 6; int arrayConstants[ ] = new int [CONST_DEMO_SIZE ]; int index = CONST_DEMO_SIZE -1; // load the array highest to lowest element do { arrayConstants [index] = Integer.parseInt (JOptionPane. showInputDialog( "What do you want in the next array element?")); --index; } while (index >=0); // end do-while // read out array contents lowest to highest element for (int index2 = 0; index2 < CONST_DEMO_SIZE; ++index2) { System.out.println ("point 2: " + index2); // display the results System.out.println (" Element "+ index2 + " is: " + arrayConstants [index2]) ; } // end for } //end main } //end class ArrayBasicsOne Using Constants to Specify Array Size- Example Create a constant, set it as 6 Create an array, using the constant to specify the array size We can change the array size by simply changing the 6 to the size we want, an the program will continue to work correctly. If we had not used a constant, how many changes would we need to make?
9
Multi-Dimensional Arrays: Arrays of Arrays You may find that you need an array that has more than one dimension. Example: I need to record the temperature every 6 hours for 4 days. A structure that supports this might look like: Day0001-06000601-12001201-18001801-2400 165666463 266535866 363646665 46053 65 Java does NOT support multi-dimensional arrays, per se, but you can create and array of arrays The general form for a two-dimensional integer array of arrays, like above is: int arrayName [ ] [ ] = new int [no_rows] [no_cols]; In our example we create the above array, have the user populate it, and then display the result on the console You must set the size of the arrays starting left … … to right. If you use values, rather than the “new” operator when creating the array, you must populate the arrays left to right
10
Multi-Dimensional Arrays: Arrays of Arrays Example import javax.swing.*; public class ArrayBasicsMultiDim{ public static void main(String args[]){ final int CONST_NO_DAYS = 4; final int CONST_NO_MEAS = 4; int arrayDailyTemps[ ][ ] = new int [CONST_NO_DAYS][CONST_NO_MEAS]; for (int indexDay = 1; indexDay <= CONST_NO_DAYS; ++indexDay) { for (int indexMeas = 1; indexMeas <= CONST_NO_MEAS; ++indexMeas) arrayDailyTemps [indexDay-1][indexMeas-1] = Integer.parseInt ( JOptionPane. showInputDialog( "What do you want in day " + indexDay + " measure " + indexMeas +" ?")); }// end outer for for (int indexDay = 1; indexDay <= CONST_NO_DAYS; ++indexDay) { for (int indexMeas = 1; indexMeas <= CONST_NO_MEAS; ++indexMeas) System.out.println("Day " +indexDay + " measure " + indexMeas + " is: " + arrayDailyTemps[indexDay-1][indexMeas-1]); }// end outer for // System.exit(0); was left out to freeze the console so that we can // see the consol output } //end main } //end class ArrayBasicsMultiDim Create constants to set the maximum number of rows (days) and columns (measures) Create the array Load each array element by addressing it using the form arrayName[row_index][col_index]
11
Summary Associated with each array is a variable named “length” that contains the length (a.k.a. size) of the array We can specify named constants, using the reserve word “final.” Once assigned a value, they cannot be changed by the program. They make software maintenance easier by allowing us to change a value in one place, rather than having to search through the code. Normally, the name appears in uppercase. Java doesn’t have multi-dimensional arrays, but we can use an array of arrays to meet this need. We must set the element sizes from left to right.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.