ArrayList Get Method in Java: Code & Examples
The Java ArrayList class is a powerful class for array processing. When using traditional arrays, you are bound to the size and structure you create. With ArrayList, you have a lot more flexibility with arrays. One of the methods of the ArrayList class is the get method. We will use this to access an item at a specific place in the array. First, before we can harness any of the power of ArrayList, we need to import the class into our program. We do this before any main method by importing the class.
Create the following in an IDE or using replit. All other code will be entered after the public static void main statement.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
//Enter all code here
}
Next, let's create an ArrayList object for some employees. We'll set the size to 5. Unlike a standard array, you can change this later.
ArrayList <String> employees = new ArrayList <String>(5);
employees.add("Jane Eyre");
employees.add("Edmond Dantes");
employees.add("Sherlock Holmes");
employees.add("Jean Valjean");
employees.add("Holden Caulfield");
Now, in order to get an item at a specific index, we will use the get method. The method takes a single parameter, the index (often called the bucket) in the array that you want to retrieve. This number is always an integer. Remember that Java starts counting at zero. Let's take a look at the get method for retrieving the fourth item of the array we defined earlier:
//get the fourth employee
System.out.println("The Fourth Employee is " + employees.get(3));
Remember that Java starts counting array indexes at zero. This is why the fourth bucket in the list is really item 3. When the previous code is run, the following output is displayed:
![]() |
You don't necessarily need to know which index you will be accessing. In more advanced programming, you may find the need to loop through an array and check if a given item in the list has a certain value. The following code loops through our employee array and checks for Sherlock Holmes. Notice that the get method now has a variable for the parameter, i. Just remember that this value must ALWAYS be an integer!
for(int i = 0; i < employees.size(); i++) {
if(employees.get(i) == "Sherlock Holmes") {
System.out.println("Sherlock");
}
}
Just like the standard array, ArrayList will not let you reference a bucket that doesn't exist. If there are seven items in the array, you cannot use get to access an eighth. The problem is that many developer tools won't stop you. It isn't until you run the program that the error occurs. Therefore, the following get statement will compile just fine (remember we have only five employees):
//get the sixth? employees
System.out.println("The Sixth Employee is " + employees.get(5));
However, when the program is run, Java displays an out of bounds exception. It is the same error you would get with a standard array if you tried to access a bucket that is not in the list.
![]() |
The good news is that we can catch these errors before they cause the program to crash. We can use what is called a try and catch block to capture any possible errors and display a message. The get statement is put in the try section, the error in the catch section of code.
Below is an example of how to look for out of bounds exceptions. Notice how the catch statement takes a parameter. Take a look at the error message Java returned after we tried to access item 6. The error was IndexOutOfBoundsException. This is the error we want to catch before it crashes our program.
//get the sixth? employees
try {
System.out.println(" The Sixth Employee is " + employees.get(6)) ;
} catch (IndexOutOfBoundsException e) {
System.out.println("No Such Employee");
}
Instead of crashing, Java instead displays the message we have created for this scenario.
![]() |
When it comes to the possibility of errors, catching them before they become a serious problem is recommended. More specifically, if your code will take user input, you should try to trap these types of errors.
The Java ArrayList is a more powerful tool for array processing than standard arrays. The get method in this class allows you to access a specified bucket in the list. When using get, it is a good idea to enclose the statement in a try and catch block so that any out of bounds errors are trapped before they cause the program to fail.
Register to view this lesson
Unlock your education
See for yourself why 30 million people use Study.com
Become a Study.com member and start learning now.
Become a memberAlready a member? Log in
Go backResources created by teachers for teachers
I would definitely recommend Study.com to my colleagues. It’s like a teacher waved a magic wand and did the work for me. I feel like it’s a lifeline.