ArrayList Get Method in Java: Code & Examples

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

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.

ArrayList

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:


import java.util.ArrayList;


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> employee = 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:


Java ArrayList get method output


Variable Index

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


Out of Bounds

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? employee
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


To unlock this lesson you must be a Study.com Member.
Create your account

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  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support