• Array: A type of data structure that contains a collection of data
  • In Java, arrays and ArrayLists are different data structures! Don’t confuse them!
  • The data in a Java array can be primitive or referenced
  • Parts of an array: Element → one value in an array, Index → the position of the value in array (most languages, including Java, use 0 indexing)
  • Traversing an array is accessing the values inside of it You can use any sort of iteration or loop to traverse an array. Recommend for loops, but you can make while loops or recursion work too if necessary
  • In the enhanced for loop, the variable element is assigned to values[0], values[1]. Specific purpose → get elements of a collection from beginning to end

6.1 - Array Syntax

  • When you declare an array variable, it’s not initialized. By default, each element in the array is value 0
  • You can also specify the initial values in an array. You don’t need to use the new operator. The compiler counts the number of initial values to determine the length of the array
  • int [] numbers = new[10]
  • Array elements start at 0
int[] array2 = {10, 9, 8, 7, 6};
//initializes array

Declare an array

  • // An array of 10 ints, all elements are initialized to be 0 int [] numbers = new[10]

  • // Sometimes it’s good to use a constant instead of a “magic number” final int LENGTH = 10 int[] numbers = new int[LENGTH]

  • // The length does not need to be constant in an array int length = in.nextInt(); double[] data = new double[length];

  • // An int array with 5 initial values int[squares] = {0, 1, 4, 9, 16};

  • // An array of 4 Strings String[] team1 = {“Daniel”, “Everitt”, “Natalie”, “Sophie”};

6.2 Traverse an Array

  • Traversing an array is accessing the values inside of it
  • You can use any sort of iteration or loop to traverse an array. - Recommend for loops, but you can make while loops or recursion work too if necessary
// Classic for loop
for(int i = 0; i < array.length; i++){
	System.out.println(array[i]);
}
// Enhanced for loop (explained on next slide)
for(int i: array){
	System.out.println(i);
}
// Sometimes you may need to traverse through an array backwards for a certain problem
for(int i = array.length - 1; i >= 0; i--){
	System.out.println(array[i]);
}

6.3 Enchanced for loops

  • You can use a basic for loop to visit all elements of an array
  • Use an enhanced for loop to visit all elements of the array
int[] values = {2, 5, 1};
int total = 0;
for (int element : values)
{
	total += element;
}
  • In the enhanced for loop, the variable element is assigned to values[0], values[1], …
  • Specific purpose → get elements of a collection from beginning to end
  • Better to use when the number of elements in the array is unknown
  • In the basic for loop, the index variable loop is being assigned as 0, 1, …
  • Better to use when we know the number of iterations

6.4 Developing algorithms using arrays

  • array.length returns how many values are in the array. Make sure to not confuse this with the last accessible index, which will always be one less than the length.
  • Ex: array.length returns 10, then the last accessible index is array[9].
  • array[i] - Accesses the array at index i.
int[] values = {2, 5, 1};
int total = 0;
for (int element : values)
{
	total += element;
}

int[] values = {2, 5, 1};
int total = 0;
for (int loop = 0; i < values.length; i++)
{
	total += values[loop];
}


public void addMembers(String[] names, int graduationYear) {
    //string with names (param 1)
    //int with the grad year (param 2)
    
    for (int i = 0; i<names.length; i++) { //repeats based on how many letters in the name 
      memberList.add(new Member(names[i], graduationYear, true));

      return memberList; 
    }
  }