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.
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.
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:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> myArrayList = new ArrayList<String>();
}
}To add an element to an ArrayList, you can use the add() method. This method takes the element to be added as an argument:
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");
}
}To access an element in an ArrayList, you can use the get() method. The index of the element starts from 0:
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
}
}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:
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]
}
}To find the size of an ArrayList, you can use the size() method:
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
}
}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:
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);
}
}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! 🚀