📚

 > 

💻 

 > 

⌚️

Unit 6 Overview: Array

8 min readjune 18, 2024

Kashvi Panjolia

Kashvi Panjolia

Kashvi Panjolia

Kashvi Panjolia


AP Computer Science A 💻

130 resources
See Units

The Big Takeaway of this Unit

Arrays: We can use arrays to store data, and algorithms can be used to access and traverse through this data.

Exam Weighting

  • 10-15% of the test
  • Roughly 4 to 6 multiple-choice questions
  • A possible topic of FRQ #3, which may test your ability to make arrays and algorithms.

Enduring Understanding

In this unit, we will start learning about data structures, which are structures that store multiple pieces of data. You will learn about three of them in this course: 1-D Arrays in this unit, ArrayLists in Unit 7, and 2-D Arrays in Unit 8. Arrays store only one type of data, whether that be a primitive data type or a reference data type, and they are of fixed size. When used for loops, we can perform many functions with arrays and build algorithms with them, as well.

Building Computational Thinking

In this unit, you will learn three things that are important for larger programs: how to create an array, how to traverse (going through all the elements) an array, and how to manipulate the elements in an array. One of the common mistakes that you may make at first is an ArrayIndexOutOfBoundsException, which occurs when you try to access an element where none exists, but with some practice, you will be flawless with arrays!

Main Ideas for this Unit

  • Initializing Arrays
  • Accessing Elements in Arrays
  • Traversing Arrays
  • Array Algorithms

6.1 Array Creation and Access

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
Arrays are used to store a list of items, such as items in a shopping cart, a list of student names in a classroom, or a list of numbers representing student scores on a test. To declare an array in Java, you can use the following syntax:
type[] arrayName; Here, type is the type of element that the array will store (such as int, double, String, etc.) and arrayName is the name of the array. Arrays can store primitive types and objects.
For example, to create an array of integers with the name scores, you would write:
int[] scores;
To initialize an array, you can write:
type[] arrayName = new type[arraySize];
Here, arraySize is the number of elements that the array will hold. This size cannot be changed after creating the array. To create an array of 5 integers, you would write:
int[] scores = new int[5];
To access an element of an array, you can use the array name and the index of the element in square brackets. Array indices are zero-based, which means that the first element has an index of 0, the second element has an index of 1, and so on. Here is how to access the first element of the array:
scores[0];
This statement returns the first element of the scores array. Now that you have initialized the scores array, you can populate it with 5 elements by accessing each element of the array:
scores[0] = 89;
scores[1] = 87;
scores[2] = 78;
scores[3] = 97;
scores[4] = 93;
Notice that the last element in the array has an index of one less than the length of the array. This method of populating the array is tedious and inefficient for large arrays, so you can use curly brackets to populate the array at the same time you create it.
int[] scores = {89, 87, 78, 97, 93);
For this type of initialization, you must declare and initialize the array in the same line. To modify an element of an array in your code, you can use the same syntax as above:
scores[2] = 95;
Now, the third element of the array is changed from 78 to 95.
If you tried to access an element of the array that was greater than the length of the array or a negative number, you would receive an ArrayIndexOutOfBoundsException. This is an important exception to understand. The statement
scores[5] = 75;
would cause the computer to throw this exception because, while there are 5 elements in the array, the indices only go up to 4 since the indices start from 0.
If you create an array but do not explicitly populate it with values, the elements of the array will be initialized with the default values for their type.
Here is a summary of the default values for the various types in Java:
  • boolean: false
  • int: 0
  • double: 0.0
  • Object: null
Let's say you created an array of strings but never populated the array.
String[] names = new String[10];
The default value Java assigns to the 10 elements of the array is null because a String is an object. This means that if you tried to access an element of the names array, you would receive a NullPointerException.

6.2 Traversing Arrays

Let's say the teacher decided to add five points to everyone's test score because it's Computer Science Education Week. This would take the teacher a long time to accomplish because the teacher would have to write
scores[0] = scores[0] + 5;
for every one of the teacher's 25 students. Instead, this teacher could use a loop to traverse the scores array. Array traversal is the process of visiting each element in an array and performing some operation on it. Here is an example:
int[] scores = {89, 87, 91, 76, 78};
for (int i = 0; i < scores.length; i++) {
int x = scores[i] + 5;
scores[i] = x;
System.out.println(scores[i]);
}
First, a new scores array with five elements was created. Then, we used a for loop to create a variable i representing the index of each element, and set the condition that the loop should run as long as i is less than the length of scores. It is important that you don't say i is less than or equal to the length of the array because you would get an ArrayIndexOutOfBoundsException.
Next, we stored the new value of scores in an integer x. Then, we updated the element at the current index of scores to the new value. Finally, to check our work, we printed the new value of the element in scores. The output of this code is
94
92
96
81
83

6.3 Enhanced For Loop for Arrays

For loops are great to traverse through arrays, but they could still be refined. This is where the enhanced for loop comes in. The enhanced for loop is a specialized version of the for loop that is designed to simplify the process of iterating over the elements of an array. It is also known as the "for-each" loop. This is the general format of the for-each loop:
for(type element : arrayName) {
// body of the loop
}
This is read as "for each element of type __ in arrayName, do this," hence the name "for-each" loop. Let's use a for-each loop to traverse through our array of scores and print them out:
int[] scores = {77, 84, 93, 79, 93};
for(int score : scores) {
System.out.print(score + " ");
}
The output of this code will be:
77 84 93 79 93
The loop we just created can be read as "for each score of type int in scores, print the score." This is a much faster way of traversing an array. However, it is important to note that the enhanced for loop cannot be used to change the elements of the original array. Also, the enhanced for loop does not provide you with access to the index of the elements in an array, which you may need to perform some algorithms.

6.4 Developing Algorithms Using Arrays

The combination of arrays and loops is a versatile one because there are many ways you can manipulate an array using a loop. Let's write a loop to find the minimum value in the scores array.
int[] scores = {77, 84, 93, 79, 93};
int minValue = scores[0];
for(int score : scores) {
if(score < minValue) {
minValue = score;
}
}
System.out.println(minValue);
We declared the variable minValue to keep track of the minimum value in the scores array. This variable was initialized to be the first value in the array and not 0 because we are traversing an array of positive numbers, in which case initializing minValue to be 0 would result in an incorrect algorithm.
Next, we created a for-each loop to traverse through every element in scores. A for-each loop works here because we are not changing the values of the scores array -- just reading them to find the smallest one. Inside the for-each loop, we wrote an if statement checking if the current element score is less than the minimum value of the array, which right now is the first value. If it is less than the minimum value, the next line sets the current element as the new minimum value. However, 84 is not less than 77, so the loop started again.
Once the loop finished, the print statement at the end printed 77 because that is the minimum value of the array, and it happens to be the first value.
There are many other standard algorithms you will need to know in order to secure a 5 on the AP CSA exam. Here they are below:
  • Determine a minimum or maximum value
  • Compute a sum, average, or mode
  • Determine if at least one element meets certain criteria
  • Determine if all the elements meet certain criteria
  • Access all consecutive pairs of elements
  • Determine if there are duplicate elements
  • Determine the number of elements that meet certain criteria
  • Shift or rotate elements left or right
  • Reverse the order of the elements
Browse Study Guides By Unit