search
HomeJavajavaTutorialWhich data type cannot be stored in Java ArrayList?

在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. If there is any infringement, please contact admin@php.cn delete
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.

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 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

See all articles

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)