Yorkville High School Computer Science Department
Yorkville High School Computer Science Department on Facebook  Yorkville High School Computer Science Department Twitter Feed  Yorkville High School Computer Science Department on Instagram

Yorkville High School Computer Science

ASSIGNMENTS: No Current Assignments

AP Computer Science :: Lessons :: Arrays

Barron's AP Computer Science


Chapter 7:
Pages 247 - 255


Fundamentals of Java
Chapter 9
Pages 305 - 317

Arrays

A normal variable can hold only one thing at a time, but an array can hold more than one item, referred to in an array as an element, as long as they are the same type. We compared a variable to a box, but we can think of an array as a file cabinet that can hold a single element in each drawer.

Array as a File Cabinet

When an array is initialized you need to specify its size. The size of an array specifies how many elements can be stored in that array. You cannot try to access an element outside of the array's range or you will get an ArrayIndexOutOfBounds Exception. Once an array is initialized you can refer to the elements in the array by their index. The index is a number that represents an element's position with an array. Index numbering starts at 0 so an array of size 5 would have index numbers from 0 to 4. Below are some example array manipulations.

int[] abc = new int[500];
int i = 3;
          
abc[0] = 78;
abc[1] = 66;
abc[2] = (abc[0] + abc[1]) / 2;
abc[i] = 82;
abc[i + 1] = 94;
          
abc[500] = 74;

The first line declares and initializes the array. You can separate this into two lines but you cannot use the array until it has been initialized. The first two assignments for index 0 and 1 show how simple assignments are with arrays. The only difference between array assignments and variable assignments is that you need to remember to include the index number in square brackets when using arrays.

The third assignment for index 2 divides the first two elements by two, thus finding their average and storing it in index 2. abc[i] = 82 stores the number 82 in index 3 since the variable i is equal to 3 when that line is executed. Finally, the assignment abc[i + 1] = 94 stores 94 in index 4 since i + 1 equals 4.

The last line above results in a ArrayIndexOutOfBounds Exception because the array index numbers only go from 0 to 499. You would get the same error if you tried to access a negative index number.

Arrays and For Loops

Arrays go hand-in-hand with for loops since the loops can be used to easily traverse through the entire array. The partnership is made even easier because you can easily find the size of array by accessing its length property.

int[] abc = {3, 5, 1, 2, 4};
int i, j, temp;
  
for (i = abc.length - 1; i>=0; i--) {
   for (j=0; j<i; j++) {
      if (abc[j] > abc[j + 1]) {
         temp = abc[j];
         abc[j] = abc[j + 1];
         abc[j + 1] = temp;
      }
   }
}

The above for loops will sort the array abc from smallest to largest. Swapping array elements needs to be done through the use of a temporary variable. The method of sorting shown above is known as a bubble sort, which is a simple, albeit inefficient, sorting algorithm.

Yorkville High School Computer Science Department on Facebook Yorkville High School Computer Science Department Twitter Feed Yorkville High School Computer Science Department on Instagram