AP Classroom Requirements 3.D - Write program code to create, traverse, and manipulate elements in 1D array or ArrayList objects.

  • 6.1 - Array Creation and Access
  • 6.2 - Traversing Arrays
  • 6.3 - Enhanced for Loop for Arrays
  • 6.4 - Developing Algorithms Using Arrays

Primitive or Referenced?

Primitive = single variable type (int, char, float) Reference = several variable type ()

array syntax

  • declare an array variable, it's not initialized -- by default, each element in the array is value 0

Common errors: --bound errors: when you access an array value that does not exist

--uninitialized and unfilled arrays

traverse an array: --accessing the values inside of it --can use any sort of iteration or loop -recommend for loops, but while loops and recursion also work

Enhanced for loops

Developing algorithms using arrays --minium, maximums, sums, manipulating each element

array.length array[i]

Declaring Arrays

Two types, arrays and ArrayLists Primitive data types contain a single var type (int, charm float) Referenced (string array classes)

Array Syntax

int[] array = new int[10];

Traverse and Array

Main way is For loops where you go through each element of the array (enhanced or basic) Basic For loop is For when you know number of elements

public class ArrayMethods {
    private int[] values = {1, 2, 3, 4, 5};
    for (int )

}
// Write array methods for the Array Methods class below out of the options given above.
public class ArrayMethods {
    private int[] values = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    {

        int i;
    for(i = 0; i < values.length; i ++);{
        if (values[i] < values[i+1]) {
            return true;
        } else {
            return false;   
        }
    }
    }
}

Vocab

Populating

  • giving the elements in the array values

Array Bounds

  • marks start and end of array

Traversal

  • visiting each element in an array from one end of array to the other

Array Syntax

  • int [] array = new int[10];
  • int [] array2 = {10,9,8,7,6,5,4,3,2,1}; Bound Errors
  • when you access an array element that does not exist Traverse an Array
  • Any sort of loops - main way to access is for loop
  • enhanced for loop: variable element is assigned to values[0]
    • specific purpose - get elements of a collection from beg to end
    • better to use when the number of elements in the array is unknown
  • basic for loop - index varibale loop assigned as 0, 1
    • better to use when we knwo the number of iterations

Homework

Write a class called ArrayMethods that contains two methods that utilize/manipulate an array. You may choose 2 of the 4 options below to complete. Extra credit for doing all 4 options. Make sure to show test cases for all of the options you choose.

Options for hacks (Pick two):

  • Swap the first and last element in the array
  • Replace all even elements with 0
  • Return true if the array is currently sorted in increasing order
  • Return true if the array contains duplicate elements
public class ArrayMethods
 {
    private int[] values = {0, 1, 2, 3};

    public void printElements()
    {
        for(int i = 0; i < values.length; i++){
            System.out.println(values[i]);
        }
    }

    public void swapElements()
    {
        int last = values[values.length-1];
        values[values.length-1] = values[0];
        values[0] = last;
    }

    public void evenToZero()
    {
        for(int i = 0; i < values.length; i++)
        {
            if (values[i] % 2 == 0){
                values[i] = 0;
            }
        }
    }

    public static void main(String[] args)
    {
        
        System.out.println("First and Last Element Swap: ");
        ArrayMethods swapElements = new ArrayMethods();
        swapElements.swapElements();
        swapElements.printElements();

        System.out.println("Replacing All Elements w/ Zero: ");
        ArrayMethods evenToZero = new ArrayMethods();
        swapElements.evenToZero();
        swapElements.printElements();
    }
}
ArrayMethods.main(null);
First and Last Element Swap: 
3
1
2
0
Replacing All Elements w/ Zero: 
3
1
0
0