codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

51 / 65 topics
50Java Collections51Java ArrayList52Java LinkedList53Java HashSet54Java HashMap55Java Iterator
Tutorials/Java Programming/Java ArrayList
☕Java Programming

Java ArrayList

Updated 2026-05-12
15 min read

Java ArrayList

In the world of programming, collections are essential for managing groups of data efficiently. One of the most versatile and widely used collections in Java is the ArrayList. This tutorial will guide you through the basics of ArrayList, including how to add, remove, access, loop through, and sort elements. By the end of this lesson, you'll have a solid understanding of how to use ArrayList effectively in your Java applications.

Introduction

An ArrayList is a resizable array implementation of the List interface in Java. It provides dynamic arrays that can grow or shrink as needed. Unlike traditional arrays, which have a fixed size, ArrayList allows you to add or remove elements dynamically, making it highly flexible for various programming scenarios.

In this tutorial, we'll explore the core functionalities of ArrayList, including:

  • Adding Elements: How to insert elements into an ArrayList.
  • Removing Elements: How to delete elements from an ArrayList.
  • Accessing Elements: How to retrieve elements by their index.
  • Looping Through Elements: How to iterate over all elements in an ArrayList.
  • Sorting Elements: How to sort the elements of an ArrayList.

Understanding these operations will enable you to manage collections of data efficiently and effectively.

Core Content

1. Creating an ArrayList

To use an ArrayList, you first need to import it from the java.util package. Here's how you can create an ArrayList:

CreateArrayList.java
1import java.util.ArrayList;
2
3public class CreateArrayList {
4 public static void main(String[] args) {
5 // Creating an ArrayList of Strings
6 ArrayList<String> list = new ArrayList<>();
7
8 System.out.println("Initial size: " + list.size());
9 };
10}
Output
Initial size: 0

2. Adding Elements

You can add elements to an ArrayList using the add() method. This method appends the specified element to the end of the list.

AddElements.java
1import java.util.ArrayList;
2
3public class AddElements {
4 public static void main(String[] args) {
5 ArrayList<String> list = new ArrayList<>();
6
7 // Adding elements
8 list.add("Apple");
9 list.add("Banana");
10 list.add("Cherry");
11
12 System.out.println("List after adding elements: " + list);
13 }
14}
Output
List after adding elements: [Apple, Banana, Cherry]

3. Removing Elements

To remove an element from an ArrayList, you can use the remove() method. This method removes the first occurrence of the specified element.

RemoveElements.java
1import java.util.ArrayList;
2
3public class RemoveElements {
4 public static void main(String[] args) {
5 ArrayList<String> list = new ArrayList<>();
6
7 // Adding elements
8 list.add("Apple");
9 list.add("Banana");
10 list.add("Cherry");
11
12 // Removing an element
13 list.remove("Banana");
14
15 System.out.println("List after removing 'Banana': " + list);
16 }
17}
Output
List after removing 'Banana': [Apple, Cherry]

4. Accessing Elements

You can access elements in an ArrayList using the get() method by providing the index of the element.

AccessElements.java
1import java.util.ArrayList;
2
3public class AccessElements {
4 public static void main(String[] args) {
5 ArrayList<String> list = new ArrayList<>();
6
7 // Adding elements
8 list.add("Apple");
9 list.add("Banana");
10 list.add("Cherry");
11
12 // Accessing an element by index
13 String fruit = list.get(1);
14
15 System.out.println("Element at index 1: " + fruit);
16 }
17}
Output
Element at index 1: Banana

5. Looping Through Elements

You can loop through all elements in an ArrayList using a for-each loop or traditional for loop.

Using For-Each Loop

ForEachLoop.java
1import java.util.ArrayList;
2
3public class ForEachLoop {
4 public static void main(String[] args) {
5 ArrayList<String> list = new ArrayList<>();
6
7 // Adding elements
8 list.add("Apple");
9 list.add("Banana");
10 list.add("Cherry");
11
12 // Looping through elements using for-each loop
13 for (String fruit : list) {
14 System.out.println(fruit);
15 }
16 }
17}
Output
Apple
Banana
Cherry

Using Traditional For Loop

TraditionalForLoop.java
1import java.util.ArrayList;
2
3public class TraditionalForLoop {
4 public static void main(String[] args) {
5 ArrayList<String> list = new ArrayList<>();
6
7 // Adding elements
8 list.add("Apple");
9 list.add("Banana");
10 list.add("Cherry");
11
12 // Looping through elements using traditional for loop
13 for (int i = 0; i < list.size(); i++) {
14 System.out.println(list.get(i));
15 }
16 }
17}
Output
Apple
Banana
Cherry

6. Sorting Elements

To sort the elements of an ArrayList, you can use the Collections.sort() method from the java.util package.

SortElements.java
1import java.util.ArrayList;
2import java.util.Collections;
3
4public class SortElements {
5 public static void main(String[] args) {
6 ArrayList<String> list = new ArrayList<>();
7
8 // Adding elements
9 list.add("Cherry");
10 list.add("Apple");
11 list.add("Banana");
12
13 // Sorting the list
14 Collections.sort(list);
15
16 System.out.println("Sorted List: " + list);
17 }
18}
Output
Sorted List: [Apple, Banana, Cherry]

Practical Example

Let's create a practical example that demonstrates how to use an ArrayList to manage a collection of student names. We'll add some students, remove one, access another by index, loop through all students, and sort the list.

StudentList.java
1import java.util.ArrayList;
2import java.util.Collections;
3
4public class StudentList {
5 public static void main(String[] args) {
6 ArrayList<String> students = new ArrayList<>();
7
8 // Adding students
9 students.add("Alice");
10 students.add("Bob");
11 students.add("Charlie");
12
13 System.out.println("Initial list of students: " + students);
14
15 // Removing a student
16 students.remove("Bob");
17 System.out.println("List after removing 'Bob': " + students);
18
19 // Accessing a student by index
20 String firstStudent = students.get(0);
21 System.out.println("First student in the list: " + firstStudent);
22
23 // Looping through all students
24 System.out.println("All students:");
25 for (String student : students) {
26 System.out.println(student);
27 }
28
29 // Sorting the list of students
30 Collections.sort(students);
31 System.out.println("Sorted list of students: " + students);
32 }
33}
Output
Initial list of students: [Alice, Bob, Charlie]
List after removing 'Bob': [Alice, Charlie]
First student in the list: Alice
All students:
Alice
Charlie
Sorted list of students: [Alice, Charlie]

Summary

Here's a quick summary of what we've learned about ArrayList:

OperationDescription
AddingUse add() to insert elements at the end.
RemovingUse remove() to delete elements by value or index.
AccessingUse get(index) to retrieve elements by their position.
LoopingUse for-each loop or traditional for loop to iterate over all elements.
SortingUse Collections.sort() to sort the elements in ascending order.

Understanding these operations will help you manage collections of data efficiently and effectively in your Java applications.

What's Next?

Now that you have a good grasp of ArrayList, it's time to explore another important collection type: LinkedList. In the next tutorial, we'll dive into how LinkedList works, its advantages, and how to use it in various scenarios. Stay tuned!


PreviousJava CollectionsNext Java LinkedList

Recommended Gear

Java CollectionsJava LinkedList