Friday, February 17, 2023
HomeSoftware EngineeringThe way to create ArrayList from Array in Java

The way to create ArrayList from Array in Java

The issue

When you have a standard array, that appears one thing like the next:

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

And also you want to convert this to an ArrayList:

ArrayList<Ingredient> arraylist = ???;

..then you are able to do the next!

The answer

The method

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

The way to use it

In fact you can at all times simplify this one additional immediately as:

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

The way to previous Java

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

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



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments