Sunday, December 3, 2023
HomeSoftware EngineeringMethods to create ArrayList from Array in Java

Methods to create ArrayList from Array in Java

The issue

When you’ve got a conventional array, that appears one thing like the next:

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

And also you wish to convert this to an ArrayList:

ArrayList<Factor> arraylist = ???;

..then you are able to do the next!

The answer

The approach

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

Methods to use it

In fact you may at all times simplify this one additional straight as:

Record<ClassName> record = Arrays.asList(array)

Methods to outdated Java

If you’re caught within the outdated world of Java, you’ll be able to fall again to:

Record<ClassName> record  = new ArrayList<ClassName>(Arrays.asList(array));


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments