Lesson 3
Adventures in Java: Unveiling ArrayLists
Lesson Overview

Welcome aboard our Java Voyage! Today, we're learning about ArrayLists. They're similar to arrays but can resize dynamically and also have many additional ways to simplify our lives. By the end, you'll know how to create and manipulate an ArrayList and understand when it's preferable to use arrays.

Creating an ArrayList in Java

ArrayLists are akin to a flexible roster of interstellar explorers; their size changes as we encounter or lose crew members. ArrayLists, part of Java's Collections Framework, elevate arrays to another level by providing more flexibility.

Constructing an ArrayList is akin to assembling a crew list. As shown, we declare a variable of type ArrayList:

Java
1List<Integer> crewMembers = new ArrayList<>(); // creating an empty list 2List<Integer> members = Arrays.asList(1, 2, 3, 4, 5); // creating a pre-defined list

Here:

  • List<Integer> is a general type for all lists of integers
  • crewMembers is a list name
  • new ArrayList<>() creates an instance of the list of integers. Note the <> part denoting that ArrayList can be of any type (Float, String, etc.), but we already specified the type when mentioning it'd be List<Integer> at the beginning of the definition.
  • Arrays.asList declares a pre-defined list of integers.

Did you notice we used List<Integer> as a type, not ArrayList<Integer>? This is called abstraction, as List<Integer> is an interface for all lists. There is no need to understand it in detail for now, though - we'll cover it in the next courses!

Accessing ArrayList Elements

Now, let's engage with our crew. ArrayLists provide methods such as:

  • add(element) - to add a new element to the end of the list.
  • get(int index) - for accessing element at the given position index, starting from 0, as in arrays.
  • set(index, value) - for updating the value at the given position index, with a new value.
  • remove(index) - remove the element at the given position index.
  • size() - is used to determine how many elements are in the list.
Java
1crewMembers.add(101); // Add a crew member 2crewMembers.add(102); // Add another crew member 3 4System.out.println(crewMembers.get(0)); // Output: 101. Retrieves the first crew member 5System.out.println(crewMembers.size()); // Output: 2. Finds the total number of crew members 6System.out.println(crewMembers); // Output: [101, 102] 7 8// Removing the 2nd element, the `crewMembers` list now contains a single element 101 9crewMembers.remove(1); 10System.out.println(crewMembers); // Output: [101]
Multidimensional ArrayLists

Now, let's add another dimension to our ArrayList, creating a roster of crew members. This is akin to building a multidimensional ArrayList in Java. Let's say we're grouping our crew members based on the tasks they do.

Just as we created a multidimensional array of integers before, we can create an ArrayList of ArrayLists. Essentially, each element of the outer ArrayList can hold another ArrayList, and these nested ArrayLists can also have different sizes (which is not possible for multidimensional arrays):

Java
1// Step 1: Create the inner ArrayLists 2List<String> pilots = new ArrayList<>(); // First group, the pilots 3pilots.add("Sarah"); 4pilots.add("James"); 5pilots.add("John"); 6 7List<String> engineers = new ArrayList<>(); // Second group, the engineers 8engineers.add("Nina"); 9engineers.add("Tom"); 10 11// Step 2: Add these groups to an outer ArrayList 12List<List<String>> crewGroups = new ArrayList<>(); // The crew groups 13crewGroups.add(pilots); // Adding the pilot group 14crewGroups.add(engineers); // Adding the engineer group 15 16// Let's see our crew groupings 17System.out.println(crewGroups); // Output: [[Sarah, James, John], [Nina, Tom]]

This piece of code defines two groups: one for pilots and another for engineers. Then these groups are added to an ArrayList called crewGroups. Each item of crewGroups is an ArrayList itself, therefore achieving the multidimensional structure. Printing crewGroups should give you [[Sarah, James, John], [Nina, Tom]], showing our well-arranged crew groups, ready for interstellar exploration! You can notice that nested arrays have different sizes - 3 and 2, respectively, which wasn't possible with multidimensional arrays before.

Another way of doing the same thing in a shorter way is using Arrays.asList:

Java
1List<List<String>> crewGroups = Arrays.asList( 2 Arrays.asList("Sarah", "James", "John"), 3 Arrays.asList("Nina", "Tom") 4); 5System.out.println(crewGroups); // Output: [[Sarah, James, John], [Nina, Tom]]

See? Fairly simple!

Comparing ArrayLists with Arrays of Primitive Types

ArrayLists can grow or shrink flexibly. On the other hand, arrays have a fixed size. Thus, if you're dealing with changeable elements (like our dynamic crew), opt for an ArrayList. But for fixed-sized elements, like a preplanned list of planets to visit, an array will suffice.

Lesson Summary and Announcement of Practice Exercises

Bravo! You've just deciphered ArrayLists. Now you know how to create one, manipulate its elements, and decide when it's preferable to arrays. Look forward to further exploration in our knowledge galaxy and enjoyable practice exercises! Shall we continue our journey?

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.