HomeSearch

Java ArrayList int, Integer Example

This Java tutorial uses an ArrayList of Integer values to store int values. An ArrayList cannot store ints.
ArrayList, int. An ArrayList contains many elements. But in Java 8 it cannot store values. It can hold classes (like Integer) but not values (like int).
To place ints in ArrayList, we must convert them to Integers. This can be done in the add() method on ArrayList. Each int must added individually.
First example. Here we have an int array. It has 3 values in it. We create an ArrayList and add those ints as Integers in a for-loop.

Add: The add() method receives an Integer. And we can pass an int to this method—the int is cast to an Integer.

Casts
Java program that uses ArrayList of Integer values import java.util.ArrayList; public class Program { public static void main(String[] args) { int[] ids = {-3, 0, 100}; ArrayList<Integer> values = new ArrayList<>(); // Add all the ints as Integers with add. // ... The ints are cast to Integer implicitly. for (int id: ids) { values.add(id); } System.out.println(values); // The collections have the same lengths. System.out.println(values.size()); System.out.println(ids.length); } } Output [-3, 0, 100] 3 3
Compile error. We cannot specify int as the type of an ArrayList. An int is not a "ReferenceType." Instead we must use Integer—and add only Integers to this collection.
Java program that causes insert Dimensions error import java.util.ArrayList; public class Program { public static void main(String[] args) { // This does not compile. // ... Integer must be used, not int. ArrayList<int> test = new ArrayList<>(); } } Output Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error, insert "Dimensions" to complete ReferenceType at Program.main(Program.java:8)
AddAll error. We cannot pass an int array to the Collections.addAll method to add Integers to an ArrayList. An Integer array can be used.

Note: To add ints we need some extra steps to enable conversion from int to Integer. A for-loop with add() is effective.

Java program that causes unresolved compilation problem import java.util.ArrayList; import java.util.Collections; public class Program { public static void main(String[] args) { int[] ids = {-3, 0, 100}; ArrayList<Integer> values = new ArrayList<>(); // This does not compile. // ... Integer is not the same as int. Collections.addAll(values, ids); } } Output Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method addAll(Collection<? super T>, T...) in the type Collections is not applicable for the arguments (ArrayList<Integer>, int[]) at Program.main(Program.java:11)
Some research. What is an Integer? And why cannot we use int in an ArrayList? An Integer is a reference type (a class). An int is a value.

And: The ArrayList requires, in its implementation, a reference type. So int is not allowed.

Quote: The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int (Java Documentation).

A summary. With the Integer class, we store ints in an ArrayList. We cannot use int directly. This introduces some complexity to our programs.ArrayList
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls