Which Data Type Cannot be Stored in Java ArrayList?

17 Dec, 2020
The ArrayList class implements a growable array of objects. ArrayList cannot hold primitive data types such as int, double, char, and long. With the introduction to wrapped class in java that was created to hold primitive data values. Objects of these types hold one value of their corresponding primitive type(int, double, short, byte). They are used when there is a usage of the primitive data types in java structures that require objects such as JLists, ArrayLists. Now, in order to hold primitive data such as int and char in ArrayList are explained.
Primitive data types cannot be stored in ArrayList but can be in Array. ArrayList is a kind of List and List implements Collection interface. The Collection container expects only Objects data types and all the operations done in Collections, like iterations, can be performed only on Objects and not Primitive data types. An ArrayList cannot store ints. To place ints in ArrayList, we must convert them to Integers. This can be done in the add() method on ArrayList. Each int must be added individually.
Cases:
Integers in ArrayListCharacter in ArrayList
Integers in ArrayList
Character in ArrayList
Case 1: Integers in ArrayList
To place int in ArrayList, First, they are converted into Integers. This can be done in the add method on ArrayList. Each int must be added individually. For example, consider an int array. It has 3 values in it. We create an ArrayList and add those int as Integers in a for-loop.
The add() method receives an Integer. And we can pass an int to this method—the int is cast to an Integer.
 
Java
// Java Program that uses ArrayList of Integer Values  // Importing ArrayList class from// java.util packageimport java.util.ArrayList;  // Classpublic class GFG {      // Main driver method    public static void main(String[] args)    {          // Create and insert custom elements in array        int[] ids = { -3, 0, 100 };          // Create ArrayList of Integer type        ArrayList<Integer> values = new ArrayList<>();          // For- each loop to iterate over ArrayList        for (int id : ids) {              // Add all the ints as Integers with add()            // method, here the ints are cast to Integer            // implicitly.            values.add(id);        }                System.out.println(values);          // The collections have the same lengths        System.out.println(values.size());          System.out.println(ids.length);    }}
[-3, 0, 100]
3
3
Using toArray method – The toArray method copies an ArrayList’s elements to an array. 
One returns an Object array and requires casting. This version, though, returns a typed array.
Java
// Importing List and ArrayList class of// java.util package import java.util.ArrayList;import java.util.List;  public class GFG {      // Main driver method    public static void main(String[] args)    {          // Creating and initializing ArrayList        ArrayList<Integer> list = new ArrayList<>();          // Adding elements to ArrayList        // Custom inputs        list.add(7);        list.add(8);        list.add(9);          // Create an empty array and pass to toArray.        Integer[] array = {};          // Converting above array to ArrayList        // using inbuilt method - list.toArray(array)        array = list.toArray(array);          // Iterating over elements using for-each loop over        // elements of ArrayList derived from initial array        for (int elem : array) {              // Printing elements of converted ArrayList            System.out.println(elem);        }    }}
7
8
9
Case 2: Character in ArrayList
In Java ArrayList char character use-case is:
Convert them into Character
Convert string value into the character ArrayList.
Example: Java Program to convert a string to a list of characters
Java
// Java Program showcasing where Data Type// can not be stored in ArrayList  // Case 2: Java Program to convert a string// to a list of characters  // Importing List and ArrayList classes from// java.util packageimport java.util.ArrayList;import java.util.List;  // Class - Convert a String to a List of Charactersclass GFG  {    // Main driver method    public static void main(String[] args)      {        // Custom string        String string = "Geeks for Geeks";          List<Character> chars = new ArrayList<>();          for (char ch : string.toCharArray()) {              chars.add(ch);        }          System.out.println(chars);    }}
[G, e, e, k, s,  , f, o, r,  , G, e, e, k, s]
Java-ArrayList
Picked
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.