Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Working with arrays (we will use an array of double as example)"— Presentation transcript:

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

2 Accessing the elements in an array Suppose we have created an array of 5 double variables:

3 Accessing the elements in an array (cont.) The elements of the array are identified by the following variable names:

4 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.

5 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

6 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 !

7 Array index and array length (cont.) Example: a[1] // Accesses a[1] int i; i = 1; a[i] // Accesses a[1]

8 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]

9 Array index and array length (cont.) 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

10 Array index and array length (cont.) 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 }

11 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

12 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 ); }

13 Example: printing all elements in an array (cont.) Output: 0123401234

14 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

15 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] ); }

16 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

17 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

18 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

19 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: The Brute Force Search technique: Brute force search 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.

20 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.

21 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

22 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

23 The brute force search in an array The following for-loop can be used to examine every element in an array: The variable i is used as array index. (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] }

24 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: The reason for this special allowance to avoid Murphy's law (i.e.: what can go wrong, will) (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] }

25 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 for- statement. The for-statement is "self-contained".

26 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 }

27 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

28 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

29 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.

30 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" }

31 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 }

32 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

33 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


Download ppt "Working with arrays (we will use an array of double as example)"

Similar presentations


Ads by Google