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: Accessing the elements in an array (cont.) • The elements of the array are identified by the following variable names: Accessing the elements in an array (cont.) • 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. Accessing the elements in an array (cont.) • 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 ! Array index and array length (cont.) • Example: a[1] // Accesses a[1] int i; i = 1; a[i] // Accesses a[1] Array index and array length (cont.) int i; i = 2; a[i] // Accesses a[2] int i; i = 2; a[2*i] // Accesses a[4] Array index and array length (cont.) • Each array variable in Java stores the length of the array • The array length: • To access the length (information) of the array a, use the expression: a.length Array index and array length (cont.) • Example: public class Length { public static void main(String[] args) { double[] a = new double[5]; double[] b = new double[10]; // Define an array of 5 elements // Define an array of 10 elements System.out.println( a.length ); // Prints 5 System.out.println( b.length ); // Prints 10 } } Array index and array length (cont.) • Example Program: (Demo above code) – Prog file: http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/09/P rogs/Length.java • 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 ); } } } Example: printing all elements in an array (cont.) • Output: 0 1 2 3 4 Example: printing all elements in an array (cont.) • 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 Example: printing all elements in an array (cont.) • 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] ); } } } Example: printing all elements in an array (cont.) • 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: printing all elements in an array (cont.) • Example Program: (Demo above code) – Prog file: http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/09/P rogs/Print2.java • 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 basic problem solving technique in Computer Science: brute force search (cont.) • 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. A basic problem solving technique in Computer Science: brute force search (cont.) • Warning: • This is but one half of the technique, The other half is maintaining the correct information. A basic problem solving technique in Computer Science: brute force search (cont.) • 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 A basic problem solving technique in Computer Science: brute force search (cont.) • 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) Special syntax in Java to define indexing variables in for-statements (cont.) • 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 forstatement. • The for-statement is "self-contained". Special syntax in Java to define indexing variables in for-statements (cont.) • 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 } } Special syntax in Java to define indexing variables in for-statements (cont.) • Example Program: (Demo above code) – Prog file: http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/09/P rogs/Print3.java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Print3.java • To run: java Print3 Special syntax in Java to define indexing variables in for-statements (cont.) • 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. A specialized for-statement for accessing all elements in an array (cont.) • 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" } A specialized for-statement for accessing all elements in an array (cont.) • 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 } } } A specialized for-statement for accessing all elements in an array (cont.) • Example Program: (Demo above code) – Prog file: http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/09/P rogs/Print4.java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Print4.java • To run: java Print4 A specialized for-statement for accessing all elements in an array (cont.) • 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