200 likes | 378 Vues
Arrays I. Savitch Chapter 6.1: Introduction to Arrays. Finding the Average. int count; double sum; Scanner keyboard = new Scanner(System.in); System.out.println(“enter 7 integers”); sum = 0 for (count=0; count<7; count++) sum = sum + keyboard.nextDouble( );
E N D
Arrays I Savitch Chapter 6.1: Introduction to Arrays
Finding the Average int count; double sum; Scanner keyboard = new Scanner(System.in); System.out.println(“enter 7 integers”); sum = 0 for (count=0; count<7; count++) sum = sum + keyboard.nextDouble( ); System.out.println(“Average is” + sum/7);
Introduction to Arrays • To calculate the average of a series of numbers we need just one variable to store the value of the numbers. • However to calculate • the average, and also • the number of items below the average • All the numbers must be stored in separate variables
Introduction to Arrays, cont. • Arrays satisfy this need. • An array is an ordered collection of variables of the same type. • An array is also a class with • predefined methods and • instance variables
Array Details, cont. • The type of the elements is called the base type. • The base type of an array can be any type (i.e. primitive or class type). • The number of elements is the lengthor size of the array.
Creating and Accessing Arrays • example double[] temperature = new double[7]; is like declaring seven variables of type double, named temperature[0], temperature[1], temperature[2], temperature[3], temperature[4], temperature[5], temperature[6].
Creating and Accessing Arrays, cont. • These variables can be used just like any other variables of type double. • examples temperature[3] = 32.0; temperature[6] = temperature[3] + 5; System.out.println(temperature[6]); temperature[index+1] = 66.5; • These variables are called indexed variables, elements, or subscripted variables.
Array Declaration Syntax • syntax for creating an arrayBase_Type[] Array_Name =new Base_Type[Length]; • example int[] pressure = new int[100]; or int[] pressure; pressure = new int[100];
Brackets[] • Brackets [] serve three purposes: • creating the type name example: int[] pressure; • creating the new array pressure = new int[100]; • naming an indexed variable of the array pressure[3] = keyboard.nextInt();
Classes, Objects, and Methods • In some ways array resembles a class type (like String). Stringstr; int[] pressure; • Recall that class types have method • For example the string class has the method charAt(N) • This can be used by any instance of that class.str.charAt(5)
Instance Variables • Apart from methods, classes can also have variables. • These variables belong to each instance of the class. • Hence they are called instance variables.
The length Instance Variable • An array has only one public instance variable, namely length. • The length variable stores the number of elements the array can hold. • Using Array_Name.length typically produces clearer code than using an integer literal.
Indices and length • The indices of an array start with 0 and end with Array_Name.length-1. • When a for loop is used to step through an array, the loop control variable should start at 0 and end at length-1. • example for (lcv = 0; lcv < temperature.length; lcv++)
Array Index Out of Bounds • Every index must evaluate to an integer which is not less than 0 and not greater than Array_Name.length-1. • Otherwise, the index is said to be out of bounds or invalid. • An out-of-bounds index will produce a run-time error.
Incomplete Array Processing • Loops fail to process an entire array correctly when they • begin with an index other than 0 • end with an index other than length-1. • Examples for (i = 1; i < oops.length-1; index++) for (i = 1; i <= oops.length; index++) for (i = 0; i <= oops.length; index++) for (i = 0; i < oops.length-1; index++)
Initializing Arrays • An array can be initialized at the time it is declared using an initialiser list. • example double[] reading = {3,3, 15.8, 9.7}; • The size of the array is determined by the number of values in the initializer list.
Initializing Arrays, cont. • Uninitialized array elements are set to the default value of the base type. • The default value of the base type integer is 0. • However, it’s better to use either an initializer list or a for loop. int[] count = new int[100]; for (int i = 0, i < count.length, i++) a[i] = 0;
Class Exercise class Exercise1 { public static void main ( String[] args ) { int[] val = {0, 1, 2, 3}; sum = . . . . System.out.println("Sum of all numbers = "); System.out.println(sum); } } Complete the assignment statement so that it computes the sum of the numbers in the array.