2 ways to Create ArrayList in Java - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we will see 2 ways to create ArrayList in Java.
Java Provides 2 different ways to create an ArrayList,
Creating ArrayList using Arrays class, Arrays.asList();
Syntax :
ArrayList list = new ArrayList(Arrays.asList(Object obj1, Object obj2, Object obj3, ..,objn));
Example :
[java]
import java.util.ArrayList;
import java.util.Arrays;
public class Method1 {
public static void main(String args[]) {
ArrayList<String> fruits = new ArrayList<String>(
Arrays.asList("Apple", "Banana", "Grapes","Mango"));
System.out.println("List of all Fruits – "+fruits);
}
}
[/java]
Output :
List of all Fruits - [Apple, Banana, Grapes, Mango]
Creating ArrayList using java.util.ArrayList class constructor.
Syntax :
ArrayList fruitsList = new ArrayList();
        fruitsList.add("Banana");
        fruitsList.add("Apple");
        fruitsList.add("Grapes");
        fruitsList.add("Mango");
Example :
[java]
import java.util.ArrayList;
import java.util.Arrays;
public class Sample {
public static void main(String args[]) {
ArrayList fruitsList = new ArrayList();
fruitsList.add("Banana");
fruitsList.add("Apple");
fruitsList.add("Grapes");
fruitsList.add("Mango");
System.out.println("List of all Fruits – "+fruitsList);
}
}
[/java]
Output :
List of all Fruits - [Banana, Apple, Grapes, Mango]
Reference :
How Arrays Works in Java
How ArrayList works in Java
Happy Learning 🙂

How to Convert Iterable to Stream Java 8
5 Ways to Iterate ArrayList in Java
Java 8 How to get common elements from two lists
4 ways to create an Object in Java
Python – How to remove key from dictionary ?
Java 8 Stream Filter Example with Objects
JAXB Map to XML Conversion Example
ArrayList in Java
How to add elements to ArrayList in Java
Difference between ArrayList vs Vector in Java
How to Sort ArrayList in Java Ascending Order
How to Sort ArrayList in Java Descending Order
User defined sorting with Java 8 Comparator
Java how to convert ArrayList to Array Example
How to create Java Smiley Swing

How to Convert Iterable to Stream Java 8
5 Ways to Iterate ArrayList in Java
Java 8 How to get common elements from two lists
4 ways to create an Object in Java
Python – How to remove key from dictionary ?
Java 8 Stream Filter Example with Objects
JAXB Map to XML Conversion Example
ArrayList in Java
How to add elements to ArrayList in Java
Difference between ArrayList vs Vector in Java
How to Sort ArrayList in Java Ascending Order
How to Sort ArrayList in Java Descending Order
User defined sorting with Java 8 Comparator
Java how to convert ArrayList to Array Example
How to create Java Smiley Swing
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String