When storing a list of information, we use arrays. However, arrays have a few shortcomings.

  • They are of fixed size. We allocate a size when we first declare them.
  • It is difficult to insert or delete elements in the middle of the sequence. We have to shift all the elements that are after the one we are adding/removing.

Arrays are perfectly suitable for a variety of needs. However many use cases require the array to be mutable, ie changeable.

ArrayList is the perfect solution to this problem. It behaves in many ways like an array, in that it maintains an ordered sequence of elements. However, it also has methods that allow us to add or remove elements easily. This makes it a perfect fit for applications that have a list of elements that can change.

Read: Have a quick look at the documentation https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html. No need to read everything, just have a look at the first few paragraphs.

You might have noticed that the documentation refers to ArrayList<E> (some other documents might use a different letter). This syntax means that ArrayList is a generic type. A generic type is one that has another associated type. For example, you can have ArrayList<String> which is an array of strings. When declaring an ArrayList, we must specify what type of ArrayList it is.

Let's start by declaring an ArrayList:

ArrayList<String> list = new ArrayList<String>();

This is a standard object declaration for the most part. Notice that the type is specified as ArrayList<String>. The generic type can be any class. String is a class, but many basic types like int and double are not; they are primitive types. You cannot declare an ArrayList of type int. If you want to use integers, Java has a Integer class you can use: https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html.

Let's add things to our list:

list.add("a");
list.add("b");

This adds items to the end of the list.

We can also add an element at a specific index:

list.add("a");
list.add("b");
list.add(1,"c");

This adds "c" at index 1, which bumps "b" one index to the right.

We can access a specific element:

String item = list.get(2);

We can also change a specific element:

list.set(2, "bee");

We can use the toString() function to print the array to the console:

Log.d("MainActivity", list.toString());

We can find the size of the ArrayList as well:

Log.d("MainActivity", list.size() + "");

We've done a quick overview of ArrayList and seen its main features. Our main motivation for this is that we are going to start learning how to display lists of information on the screen, and those are usually backed by ArrayLists.

Read: Now have a fuller read of the documentation: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html. In particular, read the method summary and get familiar with the methods in this class.

Try: Write code that uses the ArrayList methods isEmpty, contains, indexOf, and remove.

results matching ""

    No results matching ""