Initialize an ArrayList in Java

In the last post, we examined class ArrayList in Java and its significant strategies. Here we are sharing numerous ways of introducing an ArrayList with models.

ArrayList is a piece of assortment system and is available in java.util bundle. It gives us dynamic exhibits in Java. However, it could be slower than standard clusters yet can be useful in programs where bunches of control in the exhibit is required.

  • ArrayList acquires AbstractList class and executes List interface.
  • ArrayList is instated by a size, but the size can increment assuming assortment develops or recoil in the event that items are eliminated from the assortment.
  • Java ArrayList permits us to arbitrarily get to the rundown.
  • ArrayList can not be utilized for crude sorts, as int, scorch, and so on We want a covering class for such cases (see this for subtleties).ArrayList in Java should be visible as like vector in C++.

Consideration peruser! Try not to quit adapting now. Get hold of all the significant Java Foundation and Collections ideas with the Fundamentals of Java and Java Collections Course at an understudy amicable cost and become industry prepared. To finish your planning from learning a language to DS Algo and some more, if it’s not too much trouble, allude Complete Interview Preparation Course.

The following are the different techniques to introduce an ArrayList in Java:

Below are the various methods to initialize an ArrayList in Java:

  1. Initialization with add()

    Syntax:

  2. ArrayList<Type> str = new ArrayList<Type>();
           str.add("Geeks");
           str.add("for");
           str.add("Geeks");
    

    Examples:

    // Java code to illustrate initialization
    // of ArrayList using add() method
     
    import java.util.*;
     
    public class GFG {
        public static void main(String args[])
        {
     
            // create a ArrayList String type
            ArrayList<String> gfg = new ArrayList<String>();
     
            // Initialize an ArrayList with add()
            gfg.add("Geeks");
            gfg.add("for");
            gfg.add("Geeks");
     
            // print ArrayList
            System.out.println("ArrayList : " + gfg);
        }
    }
    Output:

    ArrayList : [Geeks, for, Geeks]
    

    Examples: Using shorthand version of this method

    // Java code to illustrate initialization
    // of ArrayList using add() method
     
    import java.util.*;
     
    public class GFG {
        public static void main(String args[])
        {
     
            // create a ArrayList String type
            // and Initialize an ArrayList with add()
            ArrayList<String> gfg = new ArrayList<String>() {
                {
                    add("Geeks");
                    add("for");
                    add("Geeks");
                }
            };
     
            // print ArrayList
            System.out.println("ArrayList : " + gfg);
        }
    }
    Output:

    ArrayList : [Geeks, for, Geeks]
    
  3. Initialization using asList()

    Syntax:

    ArrayList<Type> obj = new ArrayList<Type>(
          Arrays.asList(Obj A, Obj B, Obj C, ....so on));
    

    Examples:

    // Java code to illustrate initialization
    // of ArrayList using asList method
     
    import java.util.*;
     
    public class GFG {
        public static void main(String args[])
        {
     
            // create a ArrayList String type
            // and Initialize an ArrayList with asList()
            ArrayList<String> gfg = new ArrayList<String>(
                Arrays.asList("Geeks",
                              "for",
                              "Geeks"));
     
            // print ArrayList
            System.out.println("ArrayList : " + gfg);
        }
    }
    Output:

    ArrayList : [Geeks, for, Geeks]
    
  4. Initialization using List.of() method

    Syntax:

    List<Type> obj = new ArrayList<>(
            List.of(Obj A, Obj B, Obj C, ....so on));
    

    Examples:

    // Java code to illustrate initialization
    // of ArrayList using List.of() method
     
    import java.util.*;
     
    public class GFG {
        public static void main(String args[])
        {
     
            // create a ArrayList String type
            // and Initialize an ArrayList with List.of()
            List<String> gfg = new ArrayList<>(
                List.of("Geeks",
                        "for",
                        "Geeks"));
     
            // print ArrayList
            System.out.println("ArrayList : " + gfg);
        }
    }
    Output:

    ArrayList : [Geeks, for, Geeks]
    
  5. Initialization using another Collection

    Syntax:

    List gfg = new ArrayList(collection);
    

    Examples:

    // Java code to illustrate initialization
    // of ArrayList using another collection
     
    import java.util.*;
     
    public class GFG {
        public static void main(String args[])
        {
     
            // create another collection
            List<Integer> arr = new ArrayList<>();
            arr.add(1);
            arr.add(2);
            arr.add(3);
            arr.add(4);
            arr.add(5);
     
            // create a ArrayList Integer type
            // and Initialize an ArrayList with arr
            List<Integer> gfg = new ArrayList<Integer>(arr);
     
            // print ArrayList
            System.out.println("ArrayList : " + gfg);
        }
    }

    Output:

    ArrayList : [1, 2, 3, 4, 5]

Also Read: How to set the SVG background color ?

Leave a Reply

Your email address will not be published. Required fields are marked *