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 Convert ArrayList to HashSet in Java?
ArrayList: In Java, ArrayList can have duplicates as well as maintains insertion order. HashSet: HashSet is the implementation class of Set. It does not allow duplicates and uses Hashtable internally. There are four ways to convert ArrayList to HashSet : Using constructor.Using add() method by itera
3 min read
How to Convert ArrayList to HashMap Before Java 8?
ArrayList is a resizable array. It is under the java package java.util. It gives dynamic arrays in Java. ArrayList is useful in programs where a lot of changes in the array are required but these are also slower than the standard arrays. The elements in an ArrayList can be added and removed whenever
4 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
Can Two Variables Refer to the Same ArrayList in Java?
ArrayList class in Java is basically a resizable array i.e. it can grow and shrink in size dynamically according to the values that we add to it. It is present in java.util package. Syntax: ArrayList<E> list = new ArrayList<>();An ArrayList in java can be instantiated once with the help
2 min read
Java Program to Create ArrayList From Enumeration
Enumerations serve the purpose of representing a group of named constants in a programming language. Enums are used when we know all possible values at compile-time, such as choices on a menu, rounding modes, command-line flags, etc. It is not necessary that the set of constants in an enum type stay
2 min read
Program to convert Boxed Array to Stream in Java
An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case
3 min read
Java Program to Empty an ArrayList in Java
ArrayList class is a resizable array, present in 'java.util package'. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (i.e. if you want to append/add or remove element(s) to/from an array, you have to create a new array. However, elem
3 min read