---
title: "How to get the last value of an ArrayList in Java?"  
description: "How to get the last value of an ArrayList in Java?"  
author: "Utpal Vishwas"  
published: 2023-08-21  
updated: 2023-08-23  
canonical: https://www.mindstick.com/forum/159621/how-to-get-the-last-value-of-an-arraylist-in-java  
category: "java"  
tags: ["java", "array", "array list"]  
reading_time: 2 minutes  

---

# How to get the last value of an ArrayList in Java?

How to get the last [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) of an [ArrayList](https://www.mindstick.com/articles/11973/arraylist-class-in-c-sharp) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)?

## Replies

### Reply by Aryan Kumar

Sure, there are a few ways to get the last value of an ArrayList in Java.

Here are two ways to get the last value of an ArrayList:

1. Using the `.get()` method:

Java

```plaintext
ArrayList<Integer> myList = new ArrayList<>();
myList.add(1);
myList.add(2);
myList.add(3);

// Get the last value of the ArrayList using the .get() method.
int lastValue = myList.get(myList.size() - 1);
```

In this example, the `myList` variable is an ArrayList that has the values 1, 2, and 3. The `.get()` method returns the element at the specified index. The `lastValue` variable is assigned the value of the last element in the ArrayList, which is 3.

1. Using the `.size()` and `.get()` methods:

Java

```plaintext
ArrayList<Integer> myList = new ArrayList<>();
myList.add(1);
myList.add(2);
myList.add(3);

// Get the last value of the ArrayList using the .size() and .get() methods.
int size = myList.size();
int lastValue = myList.get(size - 1);
```

In this example, the `myList` variable is an ArrayList that has the values 1, 2, and 3. The `.size()` method returns the number of elements in the ArrayList. The `.get()` method returns the element at the specified index. The `lastValue` variable is assigned the value of the last element in the ArrayList, which is 3.

Which method you use depends on your specific needs. If you need to get the last value and you don't need to do any further processing on the element, then the `.get()` method is a good option. If you need to do further processing on the element, then the `.size()` and `.get()` methods is a good option.


---

Original Source: https://www.mindstick.com/forum/159621/how-to-get-the-last-value-of-an-arraylist-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
