How to create ArrayList from Array in Java

0 min read 92 words

The problem

If you have a traditional array, that looks something like the following:

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

And you would like to convert this to an ArrayList:

ArrayList<Element> arraylist = ???;

..then you can do the following!

The solution

The technique

new ArrayList<>(Arrays.asList(array));

How to use it

Of course you could always simplify this one further directly as:

List<ClassName> list = Arrays.asList(array)

How to old Java

If you are stuck in the old world of Java, you can fall back to:

List<ClassName> list  = new ArrayList<ClassName>(Arrays.asList(array));
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Recent Posts