ArrayList Common Methods Tutorial

There are many methods in the ArrayList class that have advanced parameter list types and/or return types. I will tackle those methods on an individual basis. In this tutorial I will demonstrate how to use some of the simple common methods of the ArrayList class.

  • boolean contains(Object o) - returns true if an element contains the object argument.
  • E get(int index) - returns the element at the index argument.
  • int indexOf(Object o) - returns the index of the first occurance of the object argument.
  • boolean isEmpty() - returns true if the ArrayList has no elements.
  • int lastIndexOf(Object o) - returns the index of the last occurance of the object argument.
  • E set(int index, E element) - replaces the element at the index with the second parameter object.
  • int size() - returns the number of elements in the current ArrayList instance.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ArrayListCommonMethods
C:\Java>cd ArrayListCommonMethods
C:\Java\ArrayListCommonMethods>Notepad ArrayListCommonMethods.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


import java.util.*;

class ArrayListCommonMethods {
    public static void main(String args[]) {
        ArrayList<Number> judgesScores = new ArrayList<>();
        System.out.println("judgesScores.isEmpty() = " + judgesScores.isEmpty());
        judgesScores.addAll(Arrays.asList(9.1, 8.9, 10, 9.3, 9.1, 9.4)); // autoboxing to wrapper objects
        System.out.println("judgesScores = " + judgesScores);
        System.out.println("judgesScores.isEmpty() = " + judgesScores.isEmpty());
        System.out.println("judgesScores.size() = " + judgesScores.size());
        System.out.println("judgesScores.contains(8.7) = " + judgesScores.contains(8.7));
        System.out.println("judgesScores.contains(9.3) = " + judgesScores.contains(9.3));  
        System.out.println("judgesScores.indexOf(9.1) = " + judgesScores.indexOf(9.1));         
        System.out.println("judgesScores.lastIndexOf(9.1) = " + judgesScores.lastIndexOf(9.1));         
        System.out.println("judgesScores.indexOf(8.1) = " + judgesScores.indexOf(8.1));         
        System.out.println("judgesScores.lastIndexOf(8.1) = " + judgesScores.lastIndexOf(8.1));         
        System.out.println("judgesScores.get(3) = " + judgesScores.get(3));
        System.out.println("judgesScores.set(3, 9.99) = " + judgesScores.set(3, 9.99));      
        System.out.println("judgesScores = " + judgesScores);
    }
}

Now switch back to the command prompt (CMD) and type in javac ArrayListCommonMethods.java and press Enter.
Now type in java ArrayListCommonMethods and press Enter.


C:\Java\ArrayListCommonMethods>javac ArrayListCommonMethods.java
C:\Java\ArrayListCommonMethods>java ArrayListCommonMethods
judgesScores.isEmpty() = true
judgesScores = [9.1, 8.9, 10, 9.3, 9.1, 9.4]
judgesScores.isEmpty() = false
judgesScores.size() = 6
judgesScores.contains(8.7) = false
judgesScores.contains(9.3) = true
judgesScores.indexOf(9.1) = 0
judgesScores.lastIndexOf(9.1) = 4
judgesScores.indexOf(8.1) = -1
judgesScores.lastIndexOf(8.1) = -1
judgesScores.get(3) = 9.3
judgesScores.set(3, 9.99) = 9.3
judgesScores = [9.1, 8.9, 10, 9.99, 9.1, 9.4]


Final thoughts

None


Tutorials