Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (20.3k points)

I have an array that is initialized like:

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

I would like to convert this array into an object of the ArrayList class.

ArrayList<Element> arraylist = ???;

1 Answer

0 votes
by (40.4k points)
edited by

For this code:

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

The easy way to convert this array into an object of the ArrayList class is:

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

In this, list element has fixed size which is returned from  

asList

 But, if you want to add or remove elements from the returned list, you just need to wrap it in a new

ArrayList

Related questions

0 votes
1 answer
asked Nov 25, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Jul 27, 2019 in Java by Ritik (3.5k points)
0 votes
1 answer
asked Mar 6, 2021 in Java by dante07 (13.1k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.5k questions

29.9k answers

500 comments

99.1k users

Browse Categories

...