top of page
Writer's picturePraveen Mogan

ArrayLists: An Essential Data Structure

Another data structure you will be responsible for knowing on the AP Exam is the infamous ArrayList. These bad boys are much more versatile than Arrays and used to store a list of objects, whose size can be altered after creation as well. Basically, these structures work by initially creating a List and adding/removing objects to the list. That's it! No i'm just kidding, but that is the basic principle.


There aren't any major vocabulary terms for this unit, just understand how this structure works! :)


Note: ArrayLists are objects that store multiple values just like Arrays, but with 2 major differences: they can be resized and they can only store Objects.

 

Import for ArrayLists:

import java.util.ArrayList;


Creating an ArrayList:

ArrayList<objectType> name = new ArrayList<objectType>(size);

ArrayList<Integer> numbers = new ArrayList<Integer>();


Note: notice that the size can be left blank, meaning 0 values to start with

 

Methods:


Note: E refers to an object, any type


returnType methodName (This is the format for the below list)


int size() - returns the number of elements

ArrayList<Integer> numbers = new ArrayList<Integer>(3);

numbers.add(0);

numbers.add(1);

numbers.add(2);

numbers.size(); //3


E[] to Array() - returns all the elements in an Array

ArrayList<Integer> numbers = new ArrayList<Integer>(3);

numbers.add(0);

numbers.add(1);

numbers.add(2);

numbers.toArray()[0]; //0


E get(int index) - returns the element at the given index

ArrayList<Integer> numbers = new ArrayList<Integer>(3);

numbers.add(0);

numbers.add(1);

numbers.add(2);

numbers.get(1); //1


E remove(int index) - removes and returns the element at the given index

ArrayList<Integer> numbers = new ArrayList<Integer>(3);

numbers.add(0);

numbers.add(1);

numbers.add(2);

numbers.remove(1); //1


E set(int index, E value) - replaces the value at the given index and returns the old value at the index

ArrayList<Integer> numbers = new ArrayList<Integer>(3);

numbers.add(0);

numbers.add(1);

numbers.add(2);

numbers.set(1,3); //[1,3,2]


boolean remove(E value) - removes the 1st occurrence of value returns true of found and removed value else returns false

ArrayList<Integer> numbers = new ArrayList<Integer>(3);

numbers.add(1);

numbers.add(2);

numbers.add(3);

numbers.remove(1); //true [2,3]


boolean contains(E value) - returns true if the value is contained in the list and false otherwise

ArrayList<Integer> numbers = new ArrayList<Integer>(3);

numbers.add(1);

numbers.add(2);

numbers.add(3);

numbers.contains(0); //false


boolean add(E value) - add value to the end of the list and then returns true

ArrayList<Integer> numbers = new ArrayList<Integer>(3);

numbers.add(0);

numbers.add(1);

numbers.add(2);

numbers.add(3); // [0,1,2,3]


void add(int index, E value) - It shifts all items at the given index right one location to make room and then adds the value at the now empty index

ArrayList<Integer> numbers = new ArrayList<Integer>(3);

numbers.add(0);

numbers.add(1);

numbers.add(2);

numbers.add(1,2);// [0,2,1,2]


void clear() - removes all the values from the list

ArrayList<Integer> numbers = new ArrayList<Integer>(3);

numbers.add(0);

numbers.add(1);

numbers.add(2);

numbers.clear(); //[]


int indexOf(E value) - returns the 1st index of value, -1 if not found

ArrayList<Integer> numbers = new ArrayList<Integer>(3);

numbers.add(0);

numbers.add(1);

numbers.add(2);

numbers.indexOf(1); // 1


boolean isEmpty() - returns true when the size of the list is 0 and false otherwise

ArrayList<Integer> numbers = new ArrayList<Integer>(3);

numbers.isEmpty(); //true

 

I suggest that you find online practice problems for FRQ and continue coding ArrayLists to improve your skills.


I hope it helps and happy coding/cramming! Until next time. :)

65 views0 comments

Recent Posts

See All

Comments


Post: Blog2_Post
bottom of page