Posts

Showing posts with the label ArrayList

Java - How to create an ArrayList from an Array?

In this section, we will show you how to create an ArrayList from an Array .  Following ways can be used for converting Array to  ArrayList : By using Arrays.asList() method By using Collections.addAll() method By adding each element of the Array to ArrayList explicitly Example 1: By using Arrays.asList() method  Using asList() method from an Arrays class will convert the given Array to an ArrayList . import java.util.ArrayList ; import java.util.Arrays ; public class Main { public static void main ( String []args) { String [] strArray = { "Java" , "Kotlin" , "C" , "Go" , "Python" }; ArrayList < String > list = new ArrayList< String >( Arrays . asList ( strArray )); System . out .println( list ); } } Console Output: [Java, Kotlin, C, Go, Python] Example 2: By using Collections.addAll() method Using addAll() method from the Collections class will convert ...