πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

πŸ’Ύ

7.1 Introduction to ArrayList

3 min readβ€’november 16, 2020

Peter Cao

Peter Cao

Peter Cao

Peter Cao


AP Computer Science AΒ πŸ’»

130Β resources
See Units

The Big Takeaway Of This Unit

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

Unit Overview

Exam Weighting

  • 2.5-7.5% of the test
  • Roughly 1 to 2 multiple-choice questions
  • A possible topic of FRQ #3, which may test your ability to make ArrayLists and ArrayList algorithms.

Enduring Understanding

We will now move on to the next data structure in this course, the ArrayList. Unlike the array, ArrayLists don't have a fixed size, and we can add and remove items from them. ArrayLists are part of the Java Collections Framework, which contains different data structures for different purposes. If you decide to learn more about Java in a second course, you will learn all about these data structures!

Building Computational Thinking

ArrayLists are more versatile than arrays in many situations, but there are different methods that must be used to achieve this. Thus, it is important to know the difference between arrays and ArrayLists. Using ArrayLists, we will also learn how to sort and search elements, which are two of the most important applications.

Main Ideas for This Unit

  • Creating ArrayLists
  • ArrayList Methods
  • Traversing ArrayLists
  • ArrayList Algorithms
  • Searching
    • Sequential/Linear Search
  • Sorting
    • Selection Sort
    • Insertion Sort

7.1: Introduction to ArrayList

Introduction to the Java Collections Framework

The Java Collections Framework is a set of different data structures used to store data. They consist of several types: sets, lists, deques, and maps. Each of these has subtypes, which are separate classes. Sets are data types where each item occurs only once, and the data is unordered. Lists are collections of items that can repeat, and the data is ordered. Deques are similar to lists but items can only be added or removed at the beginning and the end, not in the middle. Maps are pairs with a key and a value to represent pairs of items such as a name and an address.
The main type of data structure that we are concerned with is lists. There are two main types of lists: LinkedLists and ArrayLists, which differ in their method of accessing elements. With LinkedLists, we can only access elements in order. If we want to access the 8th element, we have to start with the first element and then traverse through the LinkedList in order. On the other hand, with ArrayLists, we can access elements in the middle of the list much more easily, using a single method call, covered in Topic 7.2.

Generics

The Java Collections Framework, including ArrayLists, only accepts objects and reference types inside these collections and the system won't know which class the objects are until runtime. All the objects in the collection must be of a certain class. However, we can have a modifier <E> where E is a certain class which will signify the type of object in the ArrayList. For example, we can have <Integer>, <Double>, or <String>, among many other possibilities. Using the generic <E> will allow for type checking while the class is being compiled.
Java Documentation When There's a Collection:
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-xj40TwbfpIFt.png?alt=media&token=dac305a5-5da6-45ef-8351-a5aebdbd2f33

Courtesy of Know Your Meme.

Creating ArrayLists

Before we can use ArrayLists, we need to import the ArrayList class with this import statement:
import java.util.ArrayList;
Unlike arrays, we can only create ArrayLists using a constructor and not with a pre-initialized ArrayList. The most basic constructor is this:
ArrayList list = new ArrayList();
However, we can use generics to specify the types of objects in the ArrayList. This is preferred for the compiler to find errors that they may not have found otherwise. This is done using the following:
ArrayList<E> list = new ArrayList<E>();
Browse Study Guides By Unit