Home  >  Article  >  Java  >  Which data type cannot be stored in Java ArrayList?

Which data type cannot be stored in Java ArrayList?

王林
王林forward
2023-09-03 23:45:021108browse

在Java ArrayList中,哪种数据类型不能被存储?

ArrayList is a dynamic list of objects, you cannot store primitive values ​​such as int, double, char or long in ArrayList. Creating wrapper classes in java allows to save primitive data types and every object belonging to these types holds a single value for their respective primitive data type (int, double short or byte). Using primitive data types in Java structures like JLists or ArrayLists requires objects, we need to use wrappers, this article explains how to use ArrayList to store simple data types like int and char.

Primitive data types cannot be stored in ArrayList

The Collection interface only accepts Object, including ArrayList, which is a type of List. Iteration of Collection objects can only be done using object data types, not primitive data types. Therefore, you cannot store integers in an ArrayList, you must first convert them to integers using the add() method. Each int must be added one by one to achieve this.

Case

Integers in ArrayList

In order to add integers to an ArrayList, they must first be converted to integers. The add method can be used for this task, but each int must be added individually. For example, we take an int array containing 3 values. If we want to append these integers to the ArrayList as integers, then we need to carefully iterate through each of them and contain them individually using a for loop operation. Again, you can pass integer type values ​​without issue when using the add() method; however, if you sometimes need to add an integer to an ArrayList that only contains integers, you will need to cast it before adding it.

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

Use toArray method

It is used to copy the elements of ArrayList to an array. One involves converting and producing an array of objects. But this variant returns 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

Characters in ArrayList

The use cases of Java's char character ArrayList are:

Change them to characters.

The value of the string is converted into a character array list.

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]

in conclusion

Javas' ArrayList implementation provides impressive capabilities for runtime object storage and manipulation. However, some arrays cannot be used with this method and must be handled differently. In order to store specific types of data efficiently in a programming language, developers must recognize the constraints associated with such structures and choose different storage options. Programmers who understand the limitations of Java ArrayList can create efficient and effective software applications.

The above is the detailed content of Which data type cannot be stored in Java ArrayList?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete