Home >Java >javaTutorial >How to Convert an Array to an ArrayList in Java?

How to Convert an Array to an ArrayList in Java?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-22 14:05:11262browse

How to Convert an Array to an ArrayList in Java?

Creating an ArrayList from an Array

Consider the following scenario:

Element[] array = {new Element(1), new Element(2), new Element(3)};

You wish to convert this array into an ArrayList of type .

Solution:

To accomplish this conversion, utilize the Arrays.asList() method:

ArrayList<Element> arrayList = new ArrayList<>(Arrays.asList(array));

This method takes an array as input and returns a fixed-size list backed by the specified array. Modifications to the list will directly affect the elements of the original array.

The above is the detailed content of How to Convert an Array to an ArrayList in Java?. For more information, please follow other related articles on the PHP Chinese website!

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