This post is about the OCAJP exam objective “Declare and use an ArrayList of a given type“.You will be tested in the exam about various methods and syntax related to the ArrayList class. Here we would explain about the ArrayList class and its important methods. In my future posts, I would be explaining about some of the other concepts that are covered in the OCAJP exam. If you have any questions, please write your comments.
Are you looking for mock exam questions to prepare for OCAJP 8 exam, please try these free mock exam practice questions for OCAJP exam?
Here are some important points about the ArrayList class:
- ArrayList is a class present in java until package since Java 1.2 version.
- ArrayList is most widely used collections API for storing the list of objects.
- It has one direct superclass i.e AbstractList
- It has three direct subclasses(AttributeList,RoleList,RoleUnresolvedList).
- It implements List,RandomAccess,Cloneable,Serializable.
- It implements List, so you can store duplicate elements(objects) in the ArrayList.
- It implements RandomAccess, so all methods run in constant time.
- It is not synchronized.
- It is a Resizable-array implementation of the List interface.
- The size of the list is growing dynamically when more elements are added.
- Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically.
- Insertion order is preserved. It means it stores the objects as you entered.
- It can store any type of object if the ArrayList object is nongeneric. If it is generic, it stores particular type or subtype objects.
- You can store null in the ArrayList object.
Creating ArrayList Object
The arraylist class has 3 constructors.
- ArrayList()
It constructs an empty list with an initial capacity of ten.
- ArrayList(Collection<? extends E> c)
It constructs a list containing the elements of the specified collection, in the order, they are returned by the collection’s iterator. It throws NullPointerException if c is null.
- ArrayList(int initialCapacity)
It constructs an empty list with the specified initial capacity. It throws IllegalArgumentException if the specified initial capacity is negative.
Example :
import java.util.ArrayList; import java.util.LinkedList; public class ArrayListCreate { public static void main(String[] a) { ArrayList al1 = new ArrayList<>();// capacity is 10 ArrayList al2 = new ArrayList<>(20);// capacity is 20 LinkedList ll = new LinkedList<>(); ArrayList al3 = new ArrayList<>(ll);// capacity may greater than // number of objects } }
ArrayList Methods
In the subsequent sections, we would look at the various important ArrayList class’s methods and how to use them. These methods are very important for preparing OCAJP exam, in the exam, you will be tested to answer the questions that are related to operations using the ArrayList methods.
add(),addAll()
public boolean add(E e)
- It appends the specified element to the end of this list.
public void add(int index,E element)
- It inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
- It throws IndexOutOfBoundsException if the index is less than zero or greater than the size of ArrayList.
public boolean addAll(Collection<? extends E> c)
- It appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.
Example :
import java.util.ArrayList; public class ArrayListAdd { public static void main(String[] a) { ArrayList a1 = new ArrayList<>(); ArrayList a2 = new ArrayList<>(); a1.add("OC"); a1.add("JP"); a1.add(1,"A"); a1.add(3,"8"); a1.add(4,"8"); a2.add("OC"); a2.add("P8"); System.out.println(al);//prints [OC, A, JP, 8, 8] a1.addAll(a2); System.out.println(al);//prints [OC, A, JP, 8, 8, OC, P8] } }
isEmpty() and size()
public boolean isEmpty()
- It returns true if this list contains no elements.
public int size()
- It returns the number of elements in this list.
Example :
import java.util.ArrayList; public class ArrayListEmpty { public static void main(String[] a) { ArrayList al = new ArrayList<>(); al.add("OC"); al.add("JP"); al.add(1,"A"); al.add(3,"8"); al.add(4,"8"); System.out.println(al);//prints [OC, A, JP, 8, 8] System.out.println(al.isEmpty());//prints false System.out.println(al.size());//prints 5 } }
get(),set()
public E get(int index)
- It returns the element at the specified position in this list.
- It throws IndexOutOfBoundsException if the index is less than zero or greater than the size of ArrayList
public E set(int index,E element)
- It replaces the element at the specified position in this list with the specified element.
- It throws IndexOutOfBoundsException if the index is less than zero or greater than the size of ArrayList.
Example :
import java.util.ArrayList; public class ArrayListGet { public static void main(String[] a) { ArrayList al = new ArrayList<>(); al.add("OC"); al.add("JP"); al.add(1,"A"); al.add(3,"8"); al.add(4,"8"); System.out.println(al);//prints [OC, A, JP, 8, 8] al.set(1, "P"); System.out.println(al);//prints [OC, P, JP, 8, 8] System.out.println(al.get(2));//prints JP } }
remove(),clear()
public boolean remove(Object o)
- It removes the first occurrence of the specified element from this list and returns true if it is present. If the list does not contain the element, it is unchanged and returns false.
public E remove(int index)
- It removes the element at the specified position in this list and returns that element. Shifts any subsequent elements to the left (subtracts one from their indices).
- It throws IndexOutOfBoundsException if the index is less than zero or greater than the size of ArrayList.
public void clear()
- It removes all of the elements from this list.
Example :
import java.util.ArrayList; public class ArrayListRemove { public static void main(String[] a) { ArrayList al = new ArrayList<>(); al.add("OC"); al.add("JP"); al.add(1,"A"); al.add(3,"8"); al.add(4,"8"); System.out.println(al);//prints [OC, A, JP, 8, 8] al.remove("A"); al.remove(3); System.out.println(al);//prints [OC, JP, 8] al.clear(); System.out.println(al);//prints [] } }
subList()
public List subList(int fromIndex,int toIndex)
- It returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. If fromIndex and toIndex are equal, the returned list is empty. The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.
- It throws IndexOutOfBoundsException if fromIndex is less than Zero or toIndex is greater than ArrayList size.
- It also throws IllegalArgumentException if fromIndex is greater than toIndex.
Example :
public class ArrayListSubList { public static void main(String[] a) { ArrayList a1 = new ArrayList<>(); a1.add("OC"); a1.add("JP"); a1.add(1,"A"); a1.add(3,"8"); a1.add(4,"8"); System.out.println(a1.subList(1, 3));//prints [A, JP] } }
contains(),indexOf(),lastIndexOf()
public boolean contains(Object o)
- It returns true if this list contains the specified element otherwise returns false.
public int indexOf(Object o)
- It returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
public int lastIndexOf(Object o)
- It returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
Example :
public class ArrayListContain { public static void main(String[] a) { ArrayList al = new ArrayList<>(); al.add("OC"); al.add("JP"); al.add(1,"A"); al.add("OC"); al.add(3,"8"); al.add(4,"8"); System.out.println(al);//prints [OC, A, JP, 8, 8] System.out.println(al.contains("A"));//prints true System.out.println(al.indexOf("OC"));//prints 0 System.out.println(al.lastIndexOf("OC"));//prints 5 } }
Conclusion
For the OCAJP, Make sure you have a clear understanding about ArrayList methods. Remember the important ArrayList class methods declarations and their functionality. Be able to identify the correct output when multiple methods are chaining. The above all are the important methods for OCAJP.
References
If you are preparing for the OCAJP certification exam, then the following resources will be useful to you.
- JavaBeat’s OCAJP Guide
- OCAJP Mock Exams (Java Basics)
- How to prepare for OCAJP Certification Exam?
- What are the good books for OCAJP Exam?