Copyright
The Java ArrayList has many more functions than standard arrays. In this lesson, we will define the ArrayList get method, and provide working code examples to highlight its use in programming. Updated: 04/09/2025

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.

Become a member to unlock this lesson

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");
}
}


Become a member to unlock this lesson

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.


Java ArrayList out of bounds message


Become a member to unlock this lesson

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.

Become a member to unlock this lesson

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.

Become a member to unlock this lesson

Register to view this lesson

Are you a student or a teacher?

Unlock your education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a member Go back

Resources created by teachers for teachers

Over 30,000 video lessons & teaching resources—all in one place.
Video lessons
Quizzes and worksheets
Classroom integration
Lesson plans

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.

Jennifer B.
Teacher
Jennifer B.
Create an account to start this course today
Used by over 30 million students worldwide
close