Now that we've worked with arrays, let's introduce ArrayLists. ArrayLists are part of the Java Collections Framework and provide a more flexible way to work with lists of objects. Unlike arrays, ArrayLists can grow or shrink in size dynamically.
-
Open the file called ArrayListDemo.java
in your project directory.
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
// We'll add our code here
}
}
Note the import
statement at the top. This tells Java that we want to use the ArrayList class in our program.
-
Now, let's create an ArrayList of Strings. Add this line inside the main
method:
ArrayList<String> fruits = new ArrayList<>();
This creates an empty ArrayList that can hold String objects. The <String>
part is called a "generic" and specifies the type of elements the ArrayList will contain.
-
Let's add some elements to our ArrayList. Add these lines:
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
The add()
method appends elements to the end of the list.
-
Now, let's print our ArrayList. Add this code:
System.out.println("Fruits in the list:");
for (String fruit : fruits) {
System.out.println(fruit);
}
This uses an enhanced for loop to iterate through the ArrayList, similar to what we did with arrays.
Tips: You can compile and run your program at any time to see the output. Or, you can continue adding more code and run it all at the end.
-
ArrayLists have many useful methods. Let's try a few. Add this code:
System.out.println("\nNumber of fruits: " + fruits.size());
System.out.println("The second fruit is: " + fruits.get(1));
size()
returns the number of elements in the ArrayList, and get(index)
retrieves the element at the specified index.
-
We can also modify elements in an ArrayList. Add this code:
fruits.set(1, "Blueberry");
System.out.println("\nAfter changing the second fruit:");
System.out.println(fruits);
set(index, element)
replaces the element at the specified index.
-
Unlike arrays, we can easily add elements at any position in an ArrayList. Try this:
fruits.add(1, "Blackberry");
System.out.println("\nAfter adding Blackberry as the second fruit:");
System.out.println(fruits);
This inserts "Blackberry" at index 1, shifting the subsequent elements to the right.
-
We can also remove elements from an ArrayList. Add this code:
fruits.remove("Cherry");
System.out.println("\nAfter removing Cherry:");
System.out.println(fruits);
This removes the first occurrence of "Cherry" from the ArrayList.
-
Finally, let's check if certain elements are in our ArrayList:
System.out.println("\nDoes the list contain Apple? " + fruits.contains("Apple"));
System.out.println("Does the list contain Cherry? " + fruits.contains("Cherry"));
The contains()
method checks if the ArrayList contains a specific element.
-
Save the file, then compile and run the program:
javac ~/project/ArrayListDemo.java
java -cp ~/project ArrayListDemo
You should see output similar to this:
Fruits in the list:
Apple
Banana
Cherry
Number of fruits: 3
The second fruit is: Banana
After changing the second fruit:
[Apple, Blueberry, Cherry]
After removing Cherry:
[Apple, Blueberry]
Does the list contain Apple? true
Does the list contain Cherry? false
Congratulations! You've now created an ArrayList, added and removed elements, accessed elements by index, and used various ArrayList methods. ArrayLists offer more flexibility than arrays, as they can grow and shrink as needed. This makes them very useful when you don't know in advance how many elements you'll be working with.