Java ArrayList Tutorial 🎯

beginner
6 min

Java ArrayList Tutorial 🎯

Welcome to this comprehensive guide on Java ArrayList! In this tutorial, we'll delve deep into the world of ArrayLists, one of the most commonly used data structures in Java. By the end of this lesson, you'll have a solid understanding of what an ArrayList is, how it works, and how to use it effectively in your projects.

What is an ArrayList? 📝

An ArrayList is a dynamic array implementation of the List interface in Java. It is a collection of elements that can hold any type of data, and it automatically resizes itself as elements are added or removed. This makes ArrayLists extremely versatile and practical for various programming tasks.

Why use an ArrayList? 💡

  • Dynamic size: Unlike traditional arrays, ArrayLists can grow and shrink as needed, saving memory and making them more efficient.
  • Holds any data type: An ArrayList can store objects of any data type, making it a versatile choice for a wide range of applications.
  • Easy to use: ArrayLists provide several methods for common operations like adding, removing, and accessing elements, making them convenient for developers.

Creating an ArrayList 🎯

To create an ArrayList, you first need to import the ArrayList class from the java.util package. Here's an example of creating an ArrayList to store String objects:

java
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> myArrayList = new ArrayList<String>(); } }

Adding Elements to an ArrayList 🎯

To add an element to an ArrayList, you can use the add() method. This method takes the element to be added as an argument:

java
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> myArrayList = new ArrayList<String>(); myArrayList.add("Apple"); myArrayList.add("Banana"); myArrayList.add("Cherry"); } }

Accessing Elements in an ArrayList 🎯

To access an element in an ArrayList, you can use the get() method. The index of the element starts from 0:

java
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> myArrayList = new ArrayList<String>(); myArrayList.add("Apple"); myArrayList.add("Banana"); myArrayList.add("Cherry"); System.out.println(myArrayList.get(0)); // Output: Apple System.out.println(myArrayList.get(1)); // Output: Banana System.out.println(myArrayList.get(2)); // Output: Cherry } }

Removing Elements from an ArrayList 🎯

To remove an element from an ArrayList, you can use the remove() method. If you want to remove an element at a specific index, you can use the remove(int index) method:

java
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> myArrayList = new ArrayList<String>(); myArrayList.add("Apple"); myArrayList.add("Banana"); myArrayList.add("Cherry"); myArrayList.remove(1); // Removes Banana System.out.println(myArrayList); // Output: [Apple, Cherry] } }

Size of an ArrayList 🎯

To find the size of an ArrayList, you can use the size() method:

java
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> myArrayList = new ArrayList<String>(); myArrayList.add("Apple"); myArrayList.add("Banana"); myArrayList.add("Cherry"); System.out.println(myArrayList.size()); // Output: 3 } }

ArrayList Types 📝

In Java, ArrayList can only store objects. If you want to store primitive data types like integers, you should use the ArrayList<Integer> for integers, ArrayList<Double> for doubles, and so on:

java
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> myArrayListInt = new ArrayList<Integer>(); ArrayList<Double> myArrayListDouble = new ArrayList<Double>(); myArrayListInt.add(1); myArrayListInt.add(2); myArrayListInt.add(3); myArrayListDouble.add(1.5); myArrayListDouble.add(2.5); myArrayListDouble.add(3.5); } }

Quiz 🎯

Quick Quiz
Question 1 of 1

What method would you use to add an element to an ArrayList?

That's it for this lesson on Java ArrayList! By now, you should have a good understanding of what an ArrayList is, how to create one, and how to perform basic operations like adding, removing, and accessing elements. Happy coding! 🚀