Arrays

Arrays are 10-15% of the exam, so it is very important to understand the basics of arrays. Collegeboard will typically test you on:

  • Representing multiple items as array objects
  • Traversing an array by accessing the elements using iteration statements
  • Standard algorithms that utilize array traversals to perform functions

Array Basics

An array can contain primitives (int, char, etc) and object references of a class. Primitive data types have their actual values stored in contiguous memory location.

Important things to note are that:

  • Arrays are fixed in size
  • Arrays can be used as a static field, local variable, or method parameter

array

//BASIC ARRAY CONCEPTS

//Declare an array and then allocate memory
int myArray[];
myArray = new int[4];

//Declare while directly assign values
int[] myArray1 = new int[]{ 1, 6, 7, 9};

//Initialize element
myArray[0] = 2;

//Create an array of objects
Object[] arr = new Object[5];

Traversing an Array

Common ways to traverse an array include for loops and while loops.

//Basic for loop to print elements of array
int[] arr1 = {0, 6, 8, 2};

// Looping through array by incrementing value of i
//'i' is an index of array 'arr1'
for (int i = 0; i < arr1.length; i++) {

    // Print array element present at index i
    System.out.print(arr1[i] + " ");

}

/* NOTE: You may also use an enhanced for loop

for(int index: arr){
    System.out.println(index)
}

*/
0 6 8 2 
//HACK!!!
//Create a method that sets each element of an array to 0

Developing Algorithms Using Arrays

Arrays are an essential part in developing algorithms. It is important to understand how to traverse an array and the structure of an array to accomplish this.

There are lots of algorithms you can create,and CollegeBoard goes over a lot of good examples of this. They go over this in the 6.4 unit videos.

//Example finding the max in an array. 

//Finds the maximum in an array
public static int maximum(int[] array) {
    //variable that holds value of max value
    int maxValue = array[0];
    //for each number in the array..
    for (int number: array) {
      //checks if current index is greater than maxValue
      if (number > maxValue) {
        //if new max value found, replace current maxValue
        maxValue = number;
      }
    }
    return maxValue;
  }

//tester array
int[] test = {3, 5, 7, 2, 10};

//returns 10
maximum(test);

//HACK!!!
//What would we have to change in this algorithm if we wanted to find the min
10

2-D Arrays

2-D arrays are 7.5-10% of the exam, so they are also very important to know for the AP exam. Many concepts of 2-D arrays rely on a solid understanding of arrays.

A 2-D array is an array of arrays, and store data similar to a row and column fashion.

Length is determined by the number of rows in the array, which is also the number of arrays in the 2-D array.

Columns is based on the size of each row of an array and can be determined based on the number of elements in the first row.

2-D Array Basics

//create a new 2-d array
int[][] twoDArray = new int[3][3];

//access the value at row 2 column 1
twoDArray[1][0];

//get value at the end of a 2-d array
// (twoDArray.length - 1) gets how many arrays (rows)
// (twoDArray[0].length - 1) gets how many elements in first array (columns)
twoDArray[twoDArray.length - 1][twoDArray[0].length - 1];
0

Traversing 2-D Arrays

2-D arrays are traversed similarly to arrays in that you will primarily use for and while loops. However, since there are both rows and columns now, you will need to use nested for loops or nested enhanced for loops.

//Prints the 2-d array
public static void print2D(int mat[][])
    {
        // For each row of the array (goes one array at a time)
        for (int row = 0; row < twoDArray1.length; row++)
 
            //Goes through each element of the array
            for (int column = 0; column < twoDArray1[row].length; column++)
                System.out.print(mat[row][column] + " ");
    }

    int twoDArray1[][] = { { 1, 2, 3 },
                           { 4, 6, 7 },
                           { 8, 9, 10} };
 
print2D(twoDArray1);
1 2 3 4 6 7 8 9 10 

When traversing 2-D arrays it is important to note wether the code is in row-major or column-major order. This basically indicates what the outer for loop of the method is traversing.

If the outer loop is going by column, its in column major order. If the outer loop is going by row, its in row-major order.

The code example above is in row-major order

//HACK!!!!
//What does the following code segment execute?

public static void main(String[] args){
    int[][] arrayOne = new int[5][7];
    for (int row = 0; row < arrayOne.length; row++){
        for (int col = 0; col < arrayOne[row].length; col++){
            arrayOne[row][col] = row+col - 1;
        }
    }

    System.out.println(arrayOne[4][6]);
}