Which Data Type Cannot be Stored in Java ArrayList?
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
import java.util.ArrayList;
public class GFG {
public static void main(String[] args)
{
int [] ids = { - 3 , 0 , 100 };
ArrayList<Integer> values = new ArrayList<>();
for ( int id : ids) {
values.add(id);
}
System.out.println(values);
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
import java.util.ArrayList;
import java.util.List;
public class GFG {
public static void main(String[] args)
{
ArrayList<Integer> list = new ArrayList<>();
list.add( 7 );
list.add( 8 );
list.add( 9 );
Integer[] array = {};
array = list.toArray(array);
for ( int elem : array) {
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
import java.util.ArrayList;
import java.util.List;
class GFG
{
public static void main(String[] args)
{
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]
Last Updated :
17 Dec, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...