Array Basics

  • Can contain primitives (int, char, etc) and object references of a class
    • Primitive data types have their actual values stored in contiguous memory location.
  • Arrays are fixed in size
  • Arrays can be used as a static field, local variable, or method parameter
  • How an array is stored
    • 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 Arrays

  • A for loop allows you to access the index, and manipulations can be done by changing the incrementor (ie. i+=2 instead of i++)
    • can show the index
  • An enhanced for loop is simpler but you CANNOT access the index, only the value
    • shows only the value

Hack 1

Create method that sets all elements in array to n

void setArray(int[] arr, int n) {
    // your code here
    for (int i = 0; i < arr.length; i++) {
        arr[i] = n;
    }
}

int[] array = new int[10];
setArray(array, 10);

for (int i = 0; i<array.length; i++) {
    System.out.println(array[i]);
}

// Should print all 10s when working properly
10
10
10
10
10
10
10
10
10
10

Hack 2

Write an array to find the average of an array

//Finds the maximum in an array
public static int average(int[] array) {
    // put your code here
    int avg = 0;
    for (int i = 0; i < array.length; i++) {
        avg += array[i];
    }
    avg /= array.length;
    return avg;
}

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

System.out.println(average(test));
5

2D Arrays

  • A 2-D array is an array of arrays, and stores data similar to a row and column fashion
  • Length is determined by the number of rows in the array
    • 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
  • The 2D array is useful to represent a matrix
    • Accessed by the rows and columns
  • When thinking of 2D array, think of a grid
    • Think of initialization as well

Traversing a 2D Array

  • Important to think of whether the array is column or row major order
  • need to use nested for loops or nested enhanced for loops

Hack 3

Find the average number of a diagonal in a 2d array

public static int averageDiagonal (int[][] array2D) {
    // your code here
    int sum = 0, count = 0;

    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            if (i == j) { // checks if the current element is on the diagonal
                sum += arr[i][j];
                count++;
            }
        }
    }
    int average = (int) sum / count;
    return average;
}

int[][] arr = {
    {1,2,3,4,5,6},
    {7,8,9,10,11,12},
    {0,1,2,3,4,5},
    {10,11,12,13,14,15},
    {15,16,17,18,19,20}
};

System.out.println(averageDiagonal(arr));
8

2018 FRQ Question 4

This question involves reasoning about arrays of integers. You will write two static methods, both of which are in a class named ArrayTester. 2018frq

Complete method getColumn below.

/** Returns an array containing the elements of a column c of arr2D in the same order as they appear in arr2D.
* Precondition c is a valid column index in arr2D
* Postcondition arr2D is unchanged
*/
public static int[] getColumn(int [][] arr2D, int c)

2018frqpartb

Complete method isLatin below. Assume that getColumn works as specified, regardless of what you wrote in part (a). You must use getColumn, hasAllValues, and containDuplicates appropriately to receive full credit.

/** Returns true if square is a Latin square as described in parat (b);
*              false otherwise.
* Precondition square has an equal number of rows and columns
*              square has at least one row
*/
public static boolean isLatin(int[][] square)