Table of Contents
Primitive data types cannot be stored in ArrayList
Case
Integers in ArrayList
Example
Output
Use toArray method
Characters in ArrayList
in conclusion
Home Java javaTutorial Which data type cannot be stored in Java ArrayList?

Which data type cannot be stored in Java ArrayList?

Sep 03, 2023 pm 11:45 PM

在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);
   }
}
Copy after login

Output

[-3, 0, 100]
3
3
Copy after login

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);
      }
   }
}
Copy after login

Output

7
8
9
Copy after login

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);
   }
}
Copy after login

Output

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

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management? How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management? Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

See all articles