TOPIC 6.1

Main Idea: Creating 1D arrays is an important component of the exam, as are accessing array objects and being able to manipulate elements in ArrayLists.


Important Vocabulary


Constructors: These are used to initialize arrays.


Primitive Data: This is data that includes basic types like int, boolean, and double. There is no object creation involved and the actual value is copied when passed as an argument in the program.


Object-Referenced Data: This is data that includes class types which are defined by the programmer. This is a lot slower since the data is not directly stored.



Creating an Array

public class Main {
    public static void main(String[] args) {
        // Creates an array of integers with 5 elements with a constructor
        int[] array = new int[5];

        // assigns values for the array, starts at index '0'
        array[0] = 10;
        array[1] = 20;
        array[2] = 30;
        array[3] = 40;
        array[4] = 50;

        // creates a for conditional to iterate through the array with an increasing index (i++) until it reaches the last element.
        for(int i = 0; i < array.length; i++) {
            System.out.println("Element at index " + i + ": " + array[i]);
        }
    }
}

Main.main(null);
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50


TOPIC 6.2

Main Idea: We should be able to traverse through arrays via iteration with conditional loops.

Important Vocabulary


Traversal: This simply refers to accessing each object in an array.


Iteration Statements: Using for or while loops to iterate through each object in an array.


“Off by One” Errors: This is when an array has been accessed with an incorrect index. It is important to remember that Java index starts at 0, and when a different starting index is initialized, it creates an ArrayIndexOutofBoundsException error.


Traversing through an Array with a While Loop

public class ArrayTraversal {

    public static void main(String[] args) {
      
      // this sets all of the elements in the array. this is a different way of expressing in comparison to the code block in topic 6.1. 
      int[] arr = {10, 20, 30, 40, 50};
      
      int i = 0;
      
      // uses a while loop. the conditional is that as long as the index is less than the length of the array, the array will be iterated through with each value getting printed. 
      while(i < arr.length) {
        System.out.println(arr[i]);
        i++;
      }
  
    }
  
  }

  ArrayTraversal.main(null);
10
20
30
40
50


TOPIC 6.3

Main Idea: We should be able to utilize enhanced for loops to traverse through arrays.

Important Vocabulary


Enhanced For Loop: This type of loop allows for you to iterate over an array’s elements without having to pre-define an index variable.


Using an Enhanced For Loop to Traverse through Basic Array

public class ArrayTraversal2 {

    public static void main(String[] args) {
      // defining the objects in the array
      int[] arr = {10, 20, 30, 40, 50};
      
      // uses the enhanced for loop so that the index does not have to be explicitly defined
      for(int element : arr) {
        System.out.println(element);
      }
  
    }
  
  }

ArrayTraversal2.main(null);
10
20
30
40
50


TOPIC 6.4

Main Idea: Iteration and selection are crucial towards helping the computer to process all of the array objects.

public class ArraySumAlgorithm {

    public static void main(String[] args) {
      
      // defines the objects in the array 

      int[] numbers = {1, 2, 3, 4, 5};
      
      // calls method to calculate the sum of array objects 
      int sum = calculateArraySum(numbers);
      
      System.out.println("Array Integers Sum: " + sum);
  
    }
  
    public static int calculateArraySum(int[] array) {
      
      // initializes the total variable which stores the running total
      int total = 0;
      
      // using an enhanced for loop to iterate through array's elements
      for(int num : array) {
        // adds each element to the 'total' variable
        total += num;
      }
      
      return total;
    }
  
  }

ArraySumAlgorithm.main(null);
Array Integers Sum: 15