+ All Categories
Home > Documents > Working with arrays (we will use an array of double as example)

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

Date post: 04-Jan-2016
Category:
Upload: reginald-caldwell
View: 225 times
Download: 1 times
Share this document with a friend
Popular Tags:
33
Working with arrays (we will use an array of double as example)
Transcript
Page 1: Working with arrays (we will use an array of double as example)

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

Page 2: 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:

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

Accessing the elements in an array (cont.)

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

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

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.

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

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

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

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 !

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

Array index and array length (cont.)

• Example:

a[1] // Accesses a[1]

int i;

i = 1;

a[i] // Accesses a[1]

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

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]

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

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

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

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

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

Array index and array length (cont.)

• Example Program: (Demo above code)         – Prog file:

http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/09/Progs/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

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

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

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

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

• Output:

0

1

2

3

4

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

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

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

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

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

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

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

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/Progs/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

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

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

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

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.

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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/Progs/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

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

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

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

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.

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

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

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

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

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

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/Progs/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

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

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


Recommended