πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

πŸ’Ύ

7.2 ArrayList Methods

4 min readβ€’december 22, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang


AP Computer Science AΒ πŸ’»

130Β resources
See Units

Note: This will use arrayOne from Topic 6.1, which is {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.
In this course, there are a few methods from the ArrayList class that you are expected to know and use for the following four topics. We will be talking about them and their functionalities below.

Adding Elements to the End Of an ArrayList

After we call the ArrayList constructor, we have an empty ArrayList. After that, we need to add elements to this ArrayList! How? We use the following method:
  • boolean add(E obj)
This has a boolean return because this method returns true to show that the element has been successfully added.
Let's try to recreate arrayOne from the Unit 6 Guide and make it into an ArrayList!
import java.util.ArrayList;
ArrayList<Integer> integerList = new ArrayList<Integer>(); integerList.add(3); integerList.add(4); integerList.add(5); integerList.add(5); integerList.add(5); integerList.add(5); integerList.add(5); integerList.add(6); integerList.add(7); integerList.add(8); integerList.add(9); integerList.add(10);
Oh no! It seems that the author of this guide got distracted while reading other Fiveable articles and added too many 5's and forgot to add 1 and 2. Perhaps he was obsessed with getting 5s and did not want to fail his AP exams! Don't fret, the other methods we will learn will allow us to fix this ArrayList!

Accessing ArrayList Elements

Before we fix this ArrayList, we will need to learn how to access ArrayList Elements. To do this, we will utilize the following two methods:
  • int size()
  • E get(int index)
The size() method should be self-explanatory, as all it does is return the size of the array. This will be useful for the get() method.
To use the get method to get a specific element, we have to use the element's index. As in Unit 6, remember that Java is a zero-indexed language so the first item in the ArrayList has index 0 and the last item has index arrayName.size() (not to be confused with length and length() for arrays and Strings, respectively). Trying to access elements outside this range results in an ArrayIndexOutOfBoundsException. Let's have some practice by writing some code to get the even numbers from the ArrayList we made above (For reference, the ArrayList has the items 3, 4, 5, 5, 5, 5, 5, 6, 7, 8, 9, 10).

Answer

4 = integerList.get(1)
6 = integerList.get(7)
8 = integerList.get(9)
10 = integerList.get(10)

Adding Elements to the Beginning or Middle of an ArrayList

Now, let's get started with fixing our ArrayList! We need to add 1 and 2 to the beginning of our list and we shall do so with this method:
  • void add(int index, E obj)
Unlike the add() method to add an object to the end of an ArrayList, this is a void method so it doesn't return anything. However, we will know that the method hasn't worked when an IndexOutOfBoundsException is thrown because you are trying to place the item in an index that doesn't exist. Let's add 1 and 2 as follows:
integerList.add(0, 1); integerList.add(1, 2);
Note that when we add an object at an index i, all current items with current index i are shifted one to the right, which increases their indices by one and also the size of the list by one. Now our ArrayList has the items 1,2,3, 4, 5, 5, 5, 5, 5, 6, 7, 8, 9, 10.

Removing Elements From an ArrayList

Now, let's remove all the extra 5s from the ArrayList. Removing an element is similar to adding an element. We use this method to do so:
  • E remove(int index)
This method returns the removed element in addition to removing the element from the ArrayList. Now, let's try to remove all the extra 5s from our ArrayList. (For reference, the ArrayList now has the items 1, 2, 3, 4, 5, 5, 5, 5, 5, 6, 7, 8, 9, 10.)
integerList.remove(5); integerList.remove(6); integerList.remove(7); integerList.remove(8);
Oops! It seems that we used the indices from the original ArrayList and forgot the most important thing about remove(): the ArrayList now contains 1, 2, 3, 4, 5, 5, 5, 7, 9, 10. When we remove an element from an ArrayList, all the elements will shift left by one index, and the size of the ArrayList decreases by 1.
Our ArrayList now has the items 1, 2, 3, 4, 5, 5, 5, 7, 9, 10.

Modifying Elements In an ArrayList

All we have left is to change the sequence "5, 5, 7" to "6, 7, 8." This will require us to modify these three elements using the following method:
  • E set(int index, E obj)
The set() method sets the element at the index to object and returns the former object there. So to fix the ArrayList, we do the three following method calls:
integerList.set(5, 6); integerList.set(6, 7); integerList.set(7, 8);
Now we finally have an ArrayList that contains the elements from arrayOne above!
Keep in mind that these are only a few of the methods possible for ArrayLists. (These methods are the ones College Board includes on the Quick Reference they give you during the exam.) The others can be found here on the Java documentation site.
Browse Study Guides By Unit
βž•Unit 1 – Primitive Types
πŸ“±Unit 2 – Using Objects
πŸ–₯Unit 3 – Boolean Expressions & if Statements
πŸ•ΉUnit 4 – Iteration
βš™οΈUnit 5 – Writing Classes
⌚️Unit 6 – Array
πŸ’ΎUnit 7 – ArrayList
πŸ’»Unit 8 – 2D Array
πŸ–²Unit 9 – Inheritance
πŸ–±Unit 10 – Recursion

Fiveable
Fiveable
Home
Stay Connected

Β© 2024 Fiveable Inc. All rights reserved.


Β© 2024 Fiveable Inc. All rights reserved.