Section 4.4. Concepts Summary


[Page 124 (continued)]

4.4. Concepts Summary

In this chapter we have introduced arrays, loops, and comments.

4.4.1. Arrays

Arrays are used to store many pieces of data of the same type. They allow you to quickly access a particular item in the array using an index. If you couldn't use an array, you would have to create a separate variable name for each piece of data.

To declare a variable that refers to an array, use the type followed by open '[' and close ']' square brackets and then the variable name.

Pixel[] pixelArray;


This declares an array of Pixel objects. The value stored at each position in the array is a reference to a Pixel object.

Arrays are objects, and you can find out how large an array is by getting its length. pixelArray.length

Notice that this isn't a method call (there are no parentheses). This accesses a public read-only field.

You can get an element of the array using arrayReference[index]. Where the index values can range from 0 to arrayReference.length-1.

pixel = pixelArray[i];


4.4.2. Loops

Loops are used to execute a block of statements while a boolean expression is true. Most loops have variables that change during the loop which eventually cause the boolean expression to be false and the loop to stop. Loops that never stop are called infinite loops.

We introduced three types of loops in this chapter: for-each, while and for. The while loop is usually used when you don't know how many times a loop needs to execute and for loops are used when you do know how many times the loop needs to execute. The for-each loop was introduced in Java 5 (1.5). It loops through all of the elements of an array.


[Page 125]

The while loop has the keyword while followed by a boolean expression and then a block of statements between an open and close curly brace. If the boolean expression is true, the body of the loop will be executed. If the boolean expression is false, execution will continue after the body of the loop (after the close curly brace). If you just want to execute one statement in the body of the loop, then you don't need the open and close curly braces, but you should indent the statement.

while (boolean expression) {    statement1;    statement2;    ... }


If you use a while loop to execute a block of statements a set number of times you will need to declare a variable before the while and that variable will need to be changed in the body of the loop. You may also need to declare other variables that you use in the loop before the while. Don't declare variables inside the loop because you will use more memory that way.

int index = 0; // loop through all the pixels while(index < pixelArray.length) {   // get the current pixel   pixel = pixelArray[index];   // do something to the pixel   // increment the index   index++; }


The for loop does the same thing as a while loop, but it lets you declare the variables that you need for the loop, specify the boolean expression to test, and specify how to change the loop variables all in one place. This means you are less likely to forget to do each of these things.

// loop through all the pixels for (int index = 0; index < pixelArray.length; index++) {   // get the current pixel   pixel = pixelArray[index];   // do something to the pixel }



[Page 126]

4.4.3. Comments

Comments are text that the programmer adds to the code to explain the code. The compiler ignores the comments when it translates the code into a form that the computer understands.

There are several types of comments in Java. To tell the compiler to ignore all text till the end of the current line use //.

// get the current pixel pixel = pixelArray[index];


To tell the compiler to ignore several lines use a starting /* and ending */.

/* set the red value to the original value  * times the passed amount  */ pixel.setRed((int) (value * amount));


To put special comments in that can be parsed out by the javadoc utility to make html documentation use a starting /** followed by an ending */.

/**  * Method to change the red by an amount  * @param amount the amount to change the red by  */




Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
ISBN: N/A
EAN: N/A
Year: 2007
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net