📚

All Subjects

 > 

💻 

AP Comp Sci A

 > 

⌚️

Unit 6

6.1 Unit 6 Arrays Overview and 6.1 Array Creation and Access

3 min readnovember 16, 2020

peter-cao

Peter Cao

caroline49234295

Caroline Koffke


AP Computer Science A 💻

Bookmarked 2.4k • 93 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.

Unit Overview

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 array 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 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 do many things 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

Introduction to Arrays

Arrays are used to store one type of data, whether it is a primitive or reference data type. Arrays themselves are reference types. They are best thought of as a list of items with a fixed size, as arrays have a set size that cannot be changed (don’t confuse this with ArrayLists which can also be thought of as a list). Arrays are denoted by braces ({}), with items separated by commas such as the following:
{true, true, false, true}
Before we can use arrays, we need to have an import statement, which is
import java.util.Arrays;

Making Arrays

There are two ways to make arrays: using a constructor and using a pre-initialized array.
Constructor
As with other reference types, we can initialize arrays using a constructor. However, the constructor is slightly different from the constructors from Unit 5:
dataType[] arrayName = new dataType[numberOfItems];
The items in the array are initialized differently depending on the data type. Integers are initialized to 0, doubles are initialized to 0.0, booleans are initialized to false, and all reference types are initialized to null. We will talk about filling constructed lists in the next topic when we discuss traversing arrays.
Pre-initialized Arrays
We can also set an array to a pre-initialized array, similar to how we initialize strings. Here, we will initialize an array of 10 integers as follows:
int[] arrayOne = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Accessing Elements in Arrays

We access elements in arrays using bracket notation as follows: arrayName[index]. The most important thing to know is that Java is a zero-indexed language, so the first item has index 0, and not 1.
Before we talk about the index of the last item, we need to discuss how to find the array length. The array length is actually an instance variable specific to that particular array denoted as arrayName.length (not to be confused with length() for Strings). Note that this is not a method, so there are no parentheses. Thus, the last item in the array can be accessed by using arrayName.length - 1. Do not confuse this with the constructor in which we use arrayName.lengthin brackets. If we use an index outside the allowed range, we will get an ArrayIndexOutOfBoundsException.
Here is a question: how do we access the even numbers in arrayOne from above?

Answer

2 = arrayOne[1]
4 = arrayOne[3]
6 = arrayOne[5]
8 = arrayOne[7]
10 = arrayOne[9]

Was this guide helpful?

Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
🔍 Are you ready for college apps?
Take this quiz and find out!
Start Quiz
Browse Study Guides By Unit
🙏Exam Reviews
🖱Unit 10: Recursion
Unit 1: Primitive Types
📱Unit 2: Using Objects
🖥Unit 3: Boolean Expressions and if Statements
🕹Unit 4: Iteration
⚙️Unit 5: Writing Classes
⌚️Unit 6: Array
💾Unit 7: ArrayList
💻Unit 8: 2D Array
🖲Unit 9: Inheritance
FREE AP comp sci a Survival Pack + Cram Chart PDF
Sign up now for instant access to 2 amazing downloads to help you get a 5
Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
💪🏽 Are you ready for the Comp Sci exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!
START QUIZ