Initialize ArrayList of array
Create ArrayList From Array in JavaJava Java Array Java ArrayList Created: October-01, 2020 | Updated: October-17, 2021 Show
This tutorial article will introduce different ways to create ArrayList from array in Java. There are three different methods to convert an array to ArrayList in Java such as Arrays.asList(), Collections.addAll() and add(). Before proceeding with the demonstration, let us understand what is an array and ArrayList and how they differ from each other. What Is an Array in Java?An array is a collection of a fixed number of similar types of data. For example, if we want to store the data of 50 books, we can create an array of the string type that can hold 50 books. After creation, the length of the array is fixed. An array is the basic built-in functionality of Java. What Is ArrayList in Java?The ArrayList is a resizable array that stores a dynamic collection of elements found within the java.util package. Difference Between Array and ArrayList in JavaThe main difference between an array and ArrayList is that the length of an array cannot be modified or extended. To add or remove elements to/from an array, we have to create a new list. Whereas, elements can be added or removed to/from ArrayList at any point due to its resizable nature. Conversion of an Array to ArrayList Using Arrays.asList()Using Arrays.asList(), the array is passed to this method and a list object is obtained, which is again passed to the constructor of the ArrayList class as a parameter. The syntax of the Arrays.asList() is as below: ArrayListLet us follow the below example. Output: maths english history physics geography chemistryConversion of an Array to ArrayList Using Collections.addAll()This method lists all the array elements in a definite collection almost similar to Arrays.asList(). However, Collections.addAll() is much faster as compared to Arrays.asList() method on performance basis. The syntax of Collections.addAll() is as below: Collections.addAll(arraylist, new Element(1), new Element(2), new Element(3), new Element(4));Let us understand the below example. import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Method2 { public static void main(String[] args) { String[] names = {"john", "emma", "rick", "tim"}; ArrayListOutput: Conversion of an Array to ArrayList Using add()Using this method, we can create a new list and add the list elements in a much simpler way. The syntax for the add() method is as below: arraylist.add(element);Let us check the below example. import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Method3 { public static void main(String[] args) { ArrayListFollowing the above methods, we can now easily convert an array to ArrayList. Write for us DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page. Related Article - Java ArrayRelated Article - Java ArrayList
|