This video could not play here.
It may be members-only, region-restricted, or unavailable in embedded players.
The big difference between ArrayList and Arrays - 2 mins video wpgosocial
Confused about the difference between an ArrayList and Arrays? Learn more about the key differences between these two data structures, and find out which is best for your project!
Here's an example Java code that creates an ArrayList and stores 4 different object types in it:
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList myList = new ArrayList();
myList.add("apple");
myList.add(5);
myList.add(true);
myList.add(3.14);
System.out.println("The element at index 2 is: " + myList.get(2));
}
}
In this code, we create an ArrayList called `myList` which can store objects of any type using the `` syntax. We add four different types of objects to the list - a String (`"apple"`), an int (`5`), a boolean (`true`), and a double (`3.14`).
Finally, we print out the object stored at index 2 of the list using the `get()` method on the list. The output will be:
The element at index 2 is: true