Which Data Type Cannot be Stored in Java ArrayList?
Last Updated :
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 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 package
import java.util.ArrayList;
// Class
public 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);
}
}
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);
}
}
}
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 package
import java.util.ArrayList;
import java.util.List;
// Class - Convert a String to a List of Characters
class 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);
}
}
Output[G, e, e, k, s, , f, o, r, , G, e, e, k, s]
Similar Reads
How to Declare an ArrayList with Values in Java? ArrayList is simply known as a resizable array. Declaring an ArrayList with values is a basic task that involves the initialization of a list with specific elements. It is said to be dynamic as the size of it can be changed. Proceeding with the declaration of ArrayList, we have to be aware of the co
2 min read
How to Serialize ArrayList in Java? ArrayList is a class under the collections framework of java. It is present in java.util package. An ArrayList is a re-sizable array in java i.e., unlike an array, the size of an ArrayList can be modified dynamically according to our requirement. Also, the ArrayList class provides many useful method
2 min read
How Objects Can an ArrayList Hold in Java? ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java just as Vector in C++. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. In order to understan
3 min read
How to Add Element in Java ArrayList? Java ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size limit. We can add or remove elements anytime. So, it is much more flexible than the traditional array. Element can be added in Java ArrayList using add() method of java.util.ArrayList class.
2 min read
Get Enumeration Over Java ArrayList ArrayList class is a re-sizeable array that is present in java.util package. Unlike the built-in arrays, ArrayList can change their size dynamically where elements can be added and removed from an ArrayList. In java version 1, enum was not present. With advancement to version, 1.5 enum was introduce
3 min read