How to Store Integers, Doubles, etc. in ArrayList in Java

You may be reading this post because you are trying to do something like the following code, which isn’t allowed in Java because int is a “primitive type”:

ArrayList<int> intList = new ArrayList<int>();
intList.add(1);
intList.add(2);

The short answer is to use syntax such as the following:

ArrayList<Integer> intList = new ArrayList<Integer>();
intList.add(1);
intList.add(2);
 
for (int i : intList)
    System.out.println(i);

Now for more detail…two important concepts in Java are primitive types and generics. Examples of primitive types are int, double, float, etc. They are all lower case letters, and you instantiate them directly, without using a constructor.

int x = 20;
double y = 20.0;
float z = 20.0f;

Primitives are nice because they’re lightweight and easy to use. They are used a lot in the Java framework. You can make arrays out of them, too.

int[] array = new int[] {1, 2, 3, 4, 5};

Generics is a term typically used in connection with Collection objects. I’ll focus on ArrayList, which is a handy Collection class that can be used instead of arrays. As the name implies, Collections hold a collection of other objects.

By default, Collections classify the objects within them as Objects. Most classes in the Java framework (not including primitives) inherit from the Object class. So they are kind of like the grandfather of all classes. For example, if I create a class called Person, it automatically extends the Object class. If I instantiate a Person object and add it to an ArrayList, the ArrayList will consider it as any other object, not as a Person, even though it is a Person behind the scenes. In order to take advantage of the characteristics unique to the Person class, you’d have to cast it back to a Person.

public class Person
{
  private String Name;
 
  public Person(String name)
  {
    Name = name;
  }
}
 
java.util.ArrayList people = new java.util.ArrayList(); // Create a list of Objects
people.add(new Person("Joe")); // Add Joe to the list of people
Object joeObject = people.get(0); // Get Joe out, but he's an Object
Person joePerson = (Person)joeObject; // Cast Joe to a Person

As you can see, it can be a bit of a pain to have to cast from Object to Person (as is done in the last line). Plus one thing that can really create confusion is that because the ArrayList accepts any Object, you can add objects of all sorts of classes into it; so when you go to get it out, you may not know what you are getting. There may be scenarios where this is desirable, but generally this creates confusion.

This is where generics come in, which are a relatively new feature in Java and allow you to create an ArrayList that only accepts objects of a specific class (or classes that inherit from that class). This makes coding simpler and more concise.

java.util.ArrayList<Person> people = new java.util.ArrayList<Person>(); // Create a list of Objects
people.add(new Person("Joe")); // Add Joe to the list of people
Person joePerson = people.get(0); // Get Joe out

You can use generics with other types of collections, too…for example, HashMap, which is another favorite of mine.

One final thought: the name generics is not the best. Perhaps more descriptive would be specifics because it allows you to force your Collections to only consider specific classes rather than generically handling any class.

This post explains more about how you can take advantage of using custom classes in lists.

Leave a Reply