Which Data Type cannot be stored in Java ArrayList?


An ArrayList is a dynamic list of objects and you can't store primitive values like int, double, char or long in an ArrayList. The creation of wrapped class in java allows for holding of primitive data types and each object belonging to these types holds a single value for its respective primitive data type (int,double short or byte).To work with primitive data types in Java structures like JLists or ArrayLists which require objects, we need to use wrappers, and this article explains how to use ArrayList to store simple data types such as int and char.

Primitive Data types cannot be stored in ArrayList

The Collection interface only accepts Objects, including ArrayList, which is a type of List. Iterations on Collection objects may only be completed using Object data types, and not Primitive data types. As a result, it is not possible to store ints in an ArrayList and must first convert them to Integers using the add() method. One must add each int one by one to achieve this.

Cases

Integers in ArrayList

In order to add ints to an ArrayList, one must first convert them into Integers. The add method can be utilized for this task, however each int must be added separately. For instance, let's take an int array with 3 values. If we want to attach these ints to an ArrayList as Integers, then we need to iterate through each of them meticulously and include them separately using a for-loop operation. To specify again, while utilizing the add() approach, Integer type values can be passed without any issues; however, if required at times like this when adding ints to ArrayLists containing only Integers requires casting first before adding.

Example

// Java Program that uses ArrayList of Integer Values
import java.util.ArrayList;
public class demo {
   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);
   }
}

Output

[-3, 0, 100]
3
3

Using toArray Method

It is used to copy an ArrayList’s elements to an array. One involves casting and yields an Object Array. But this variant gives back a typed array.

Example

import java.util.ArrayList;
import java.util.List;
public class demo {
   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);
      }
   }
}

Output

7
8
9

Character in ArrayList

The use-case for Java’s ArrayList of char character is:

Change them to a character.

The value of a string is converted into a Character ArrayList.

Example

import java.util.ArrayList;
import java.util.List;
public class demo {
   public static void main(String [] args) {
      String string = "Computer Science";
      List<Character> chars = new ArrayList<>();
      for (char ch : string.toCharArray()) {
         chars.add(ch);
      }
      System.out.println(chars);
   }
}

Output

[C, o, m, p, u, t, e, r , S, c, i, e, n, c, e]

Conclusion

Javas' ArrayList implementation offers impressive functionality for runtime object storage and manipulation. However, There are certain arrays that cannot be utilized with this approach and must be handled differently. To effectively store specific kinds of data in programming languages developers must recognize constraints related to such structures and opt for different storage options, and efficient and effective software applications can be created by programmers who understand the limitations of Java ArrayList.

Updated on: 01-Aug-2023

345 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements