### Working with arrays (we will use an array of double as example)

• Accessing the elements in an array

• Suppose we have created an array of 5 double variables:

• The elements of the array are identified by the following variable names:

Explanation:

 The first array element is accessed through the variable name a[0] The second array element is accessed through the variable name a[1] And so on.

Note:

 The numbers 0, 1, 2, ... used to access array elements is called an (array) index The indices of the array elements (in Java) start with 0 Consequently, if an array has n elements, the index of the last element is n − 1

• Array index and array length

• The array index:

• Array index = an integer value that is used to access an array element

 You can use any integer expression as array index !

Example:

 ``` a[1] // Accesses a[1] int i; i = 1; a[i] // Accesses a[1] int i; i = 2; a[i] // Accesses a[2] int i; i = 2; a[2*i] // Accesses a[4] ```

• The array length:

• Each array variable in Java stores the length of the array

• To access the length (information) of the array a, use the expression:

 ``` a.length ```

Example:

 ``` public class Length { public static void main(String[] args) { double[] a = new double[5]; // Define an array of 5 elements double[] b = new double[10]; // Define an array of 10 elements System.out.println( a.length ); // Prints 5 System.out.println( b.length ); // Prints 10 } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Length.java To run:          java Length

• Example: printing all elements in an array

• Consider the following for-loop:

 ``` public class Print1 { public static void main(String[] args) { int i; int n = 5; for ( i = 0; i < n; i++ ) { System.out.println( i ); } } } ```

Output:

 ``` 0 1 2 3 4 ```

Within the body of the for-loop, the variable i takes on all indices of an array of length n !!!

 We can use this for-loop to print all elements in an array

• Consider the following program:

 ``` public class Print2 { public static void main(String[] args) { double[] a = { 2.3, 3.4 , 4.5, 5.6, 6.7, 7.8, 8.9 }; // 7 elements int i; System.out.println( "# elements in array: " + a.length ); System.out.println( ); System.out.println( "The array elements are:" ); for ( i = 0; i < a.length; i++ ) { System.out.println( a[i] ); } } } ```

Output of this program:

 ``` # elements in array: 7 The array elements are: 2.3 3.4 4.5 5.6 6.7 7.8 8.9 ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Print2.java To run:          java Print2

• A basic problem solving technique in Computer Science: brute force search

• Unlike humans who are intelligent but slow, in contrast, computers are dumb and fast...

 Consequently, humans and computer will solve a problem differently Humans will often discover patterns and and device shortcuts Computers on the other hand, often will try every possible answer to find the correct answer

• A commonly used technique to solve problems (perform tasks) with a computer is:

 Brute force search

The Brute Force Search technique:

 Check every instance of all possible solutions For each instance, perform the desired task (e.g., determine if it is the best solution) on that instance.

Warning:

 This is but one half of the technique, The other half is maintaining the correct information.

• Information, information, information:

• In order to perform the desired task (e.g., determine if it is the best solution), we need information

• Fact:

 A computer program (algorithm) will update some information while it examines every possible candidate solution

• The information that the computer program must maintain is problem specific.

Through experience in writing computer programs, you will develop the skills to design the necessary information

• The brute force search in an array

• The following for-loop can be used to examine every element in an array:

 ``` (a is some array (any type) ) int i; for ( i = 0; i < a.length; i++ ) { // statements in the for-loop body will be // executed ONCE for each array element a[i] } ```

The variable i is used as array index.

• Special syntax in Java to define indexing variables in for-statements

• The for-statement is used so often, that Java has extended its syntax to allow you to define the array index variable inside the for-statement as follows:

 ``` (a is some array (any type) ) for ( int i = 0; i < a.length; i++ ) { // statements in the for-loop body will be // executed ONCE for each array element a[i] } ```

The reason for this special allowance to avoid Murphy's law (i.e.: what can go wrong, will)

• The life time and scope of a variable that is defined inside a for-statement is the for-statement itself:

The index variable i can only be used inside the for-statement.

The for-statement is "self-contained".

• Example:

 ``` public class Print3 { public static void main(String[] args) { double[] a = { 2.3, 3.4 , 4.5, 5.6, 6.7, 7.8, 8.9 }; // 7 elements System.out.println( "# elements in array: " + a.length ); System.out.println( ); System.out.println( "The array elements are:" ); // System.out.println( i ); // Will cause an error: i undefined for ( int i = 0; i < a.length; i++ ) { System.out.println( a[i] ); } // System.out.println( i ); // Will cause an error: i undefined } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Print3.java To run:          java Print3

Note:

 If you uncomment the statement before or after the for-statement that uses the variable i, and re-compile, you will get an error message This error message shows you that the variable i only exists within the for-statement

• A specialized for-statement for accessing all elements in an array

• Making array processing easier to program in Java goes even further

(The designers of Java keep adding to the Java language)...

• A later version of the Java programming language introduced a specialized for-statement for array processing.

• Syntax of the specialize for-statement for array processing:

 ``` for ( elementType varName : arrayRefVar ) { // statements in the for-loop body will be // executed ONCE for each array element "arratRefVar[i]" // which is represented by the variable name "varName" } ```

Example:

 ``` public class Print4 { public static void main(String[] args) { double[] a = { 2.3, 3.4 , 4.5, 5.6, 6.7, 7.8, 8.9 }; // 7 elements System.out.println( "# elements in array: " + a.length ); System.out.println( ); System.out.println( "The array elements are:" ); for ( double x : a ) { System.out.println( x ); // print all a[i] in array a } } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Print4.java To run:          java Print4

Output: (same as Print2.java)

 ``` # elements in array: 7 The array elements are: 2.3 3.4 4.5 5.6 6.7 7.8 8.9 ```