Can you initialize ArrayList with values?
The Java ArrayList can be initialized in number of ways depending on the requirement. In this tutorial, we will learn to initialize ArrayList based on some frequently seen usecases. Show
1. Initialize ArrayList in one line1.1. Arrays.asList() – Initialize arraylist from arrayTo initialize an arraylist in single line statement, get all elements in form of array using Arrays.asList method and pass the array argument to ArrayList constructor. ArrayListProgram output. [alex, brian, charles]1.2. List.of() – Immutable list – Java 9We can use List.of() static factory methods to create immutable lists. Only drawback is that add operation is not supported in these lists. ListProgram output. [alex, brian]
2. Create ArrayList and add objects – ArrayList constructorUsing ArrayList constructor is traditional approach. We create a blank arraylist using constructor and add elements to list using add() method. We can add elements either one by one, or we can pass another collection to add all elements in one step. ArrayListProgram output. [alex, brian, charles] [alex, brian, charles, max, john, keanu]3. Initialize arraylist of listsAt times, we may need to initialize arraylist of lists. List
Program output. [10, 20, 30] [40, 50, 60] [70, 80, 90]Please note that Arrays.asList() does not return java.util.ArrayList instance. It returns java.util.Arrays$ArrayList instance instead. So if you must have an ArrayList only, then create ArrayList for Arrays.asList() instance in below manner. marks.add(new ArrayListThat’s all about to create an arraylist in Java. Drop me your questions in comments. Happy Learning !! Reference: ArrayList Java Docs Let us know if you liked the post. That’s the only way we can improve.
In this article, we will learn to initialize ArrayList with values in Java.
Many careers in tech pay over $100,000 per year. With help from Career Karma, you can find a training program that meets your needs and will set you up for a long-term, well-paid career in tech.
By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email. In the last post we discussed about class ArrayList in Java and it’s important methods. Here we are sharing multiple ways to initialize an ArrayList with examples. Method 1: Initialization using Arrays.asListSyntax: ArrayListExample: import java.util.*; public class InitializationExample1 { public static void main(String args[]) { ArrayListOutput: Elements are:[Pratap, Peter, Harsh]Method 2: Anonymous inner class method to initialize ArrayListSyntax: ArrayListExample: import java.util.*; public class InitializationExample2 { public static void main(String args[]) { ArrayListOutput: Content of Array list cities:[Delhi, Agra, Chennai]Method3: Normal way of ArrayList initializationSyntax: ArrayListExample: import java.util.*; public class Details { public static void main(String args[]) { ArrayListOutput: Books stored in array list are: [Java Book1, Java Book2, Java Book3]Method 4: Use Collections.ncopiesCollections.ncopies method can be used when we need to initialize the ArrayList with the same value for all of its elements. Syntax: count is number of elements and element is the item value ArrayListExample: Output: ArrayList items: [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]ArrayList is a part of collection framework and is present in java.util package. It provides us dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. Below are the various methods to initialize an ArrayList in Java: |