The ArrayList Class
A collection is a group of related objects, or elements, that are stored together as a single unit. An array is an example of a collection. Java also contains a collections framework , which provides classes for implementing collections. One such class is the ArrayList class, which includes methods for adding elements, deleting elements, modifying elements, and searching for elements. ArrayLists are dynamic arrays, that grow and shrink in size as elements are added and removed. The ArrayList class also has an overridden toString() method such that an arrayList will print with its elements wrapped in square brackets and separated by commas [e1 , e2 , e3 , . . . ] .
Class ArrayList (java.util.ArrayList)
The ArrayList class implements a dynamic array. A dynamic array varies in size during run time and is used in applications where the size of an array may need to grow or shrink. An ArrayList object shifts elements up one position when a new element is added at a specific index. Elements added to the end of the ArrayList do not move existing elements. Removing an element from an ArrayList also shifts elements as necessary to close the gap left by the removed element. When using an ArrayList object, it is important to understand that only objects, not primitive types, can be stored. Because the indexOf() method compares its object parameter to each element of the array, it is important that the object’s class has an appropriately overridden equals() method.
The following application creates an ArrayList object, adds elements, removes an element, and then displays the remaining elements:
import java.util.ArrayList; //import ArrayList class
public class MyFirstArrayList
{
public static void main(String[] args)
{
ArrayList cities = new ArrayList();
cities.add("New York");
cities.add("Los Angeles");
cities.add("Houston");
cities.add("Chicago");
cities.add("Philadelphia");
cities.remove(3);
for (Object name : cities)
{
System.out.println(cities);
}
}
}
The ArrayList declaration does not require an array size. An ArrayList object grows and shrinks automatically as elements are added and removed. The for-each statement traverses the ArrayList. An ArrayList converts elements to their superclass Object, which is why name is type
Object, rather than String.
The MyFirstArrayList application displays the following output:
New York
Los Angeles
Houston
Philadelphia
WRAPPER CLASSES
WRAPPER CLASSES
Primitive data types cannot be directly stored in an ArrayList because the elements in an ArrayList must be objects. The Integer and Double classes, provided with Java, are used to “wrap” primitive values in an object. The Integer and Double wrapper classes include the methods for comparing objects and for returning the value stored by the object as a primitive:
Class: Integer (java.lang.Integer)
Methods:
compareTo(Integer intObject)
returns 0 when the Integer object value is the same as intObject. A negative int is returned when the Integer object is less than intObject, and a positive int is returned when the Integer object is greater than intObject.
intValue() returns the int value of the Integer object.
Class: Double (java.lang.Double)
Methods:
compareTo(Double doubleObject)
returns 0 when the Double object value is the same as doubleObject. A negative int is returned when the Double object is less than doubleObject, and a positive int is returned when the Double object is greater than doubleObject.
doubleValue() returns the double value of the Double object.
The DataArrayList application creates an ArrayList of Integer values, compares the values, and then sums the elements:
import java.util.ArrayList;
public class DataArrayList
{
public static void main(String[] args)
{
ArrayList numbers = new ArrayList();
Integer element, element1, element2;
int sum = 0;
numbers.add(new Integer(5));
numbers.add(new Integer(2));
/* compare values */
element1 = (Integer)numbers.get(0);
element2 = (Integer)numbers.get(1);
if(element1.compareTo(element2) == 0)
{
System.out.println("The elements have the same value.");
}
else if(element1.compareTo(element2) < 0)
{
System.out.println("element1 value is less than element2.");
}
else
{
System.out.println("element1 value is greater than element2.");
}
/* sum values */
for(Object num : numbers)
{
element = (Integer)num;
sum += element.intValue();
}
System.out.println(“Sum of the elements is: “ + sum);
}
}
The StudentsList application creates an ArrayList of Students objects and compares their values by each Students’ name value.
import java.util.ArrayList;
public class StudentsList
{
public static void main(String[ ] args)
{
ArrayList<Students> roster = new ArrayList<>();
roster.add(new Students("Jose",82,"Arrays",91,"Lists"));
roster.add(new Students("Clara",89,"Arrays",95,"Lists"));
roster.add(new Students("Michael"));
if(roster.get(0).getName().compareTo(roster.get(1).getName())==0)
{
System.out.println("The Students have the same value.");
}
else
if(roster.get(0).getName().compareTo(roster.get(1).getName())< 0)
{
System.out.println("Students0 value is less than Students1.");
}
else
{
System.out.println("Students0 is greater than Students1.");
}
}
}
GOAL:
We would like to simplify this code so that
if(roster.get(0).getName().compareTo(roster.get(1).getName())==0)
will execute properly as:
if(roster.get(0).compareTo(roster.get(1))==0)
Likewise, we want it to work for greater than and less than comparisons as well. What modifications are necessary to accomplish this task?
Answer: Define a compareTo() method for the Student class.