Initialize an ArrayList in Java

 

 

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.

  • ArrayList inherits AbstractList class and implements List interface.
  • ArrayList is initialized by a size, however the size can increase if collection grows or shrink if objects are removed from the collection.
  • Java ArrayList allows us to randomly access the list.
  • ArrayList can not be used for primitive types, like int, char, etc. We need a wrapper class for such cases (see this for details).
  • ArrayList in Java can be seen as similar to vector in C++.

java-arraylist

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

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

  1. Initialization with add()

    Syntax:

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

    Examples:



    filter_none

    edit

    play_arrow

    brightness_4

    // 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

    filter_none

    edit

    play_arrow

    brightness_4

    // 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]
    
  2. Initialization using asList()

    Syntax:

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

    Examples:

    filter_none

    edit

    play_arrow

    brightness_4

    // 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]
    
  3. Initialization using List.of() method

    Syntax:

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

    Examples:

    filter_none

     

     

    brightness_4

    // 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]
    
  4. Initialization using another Collection

    Syntax:

    List gfg = new ArrayList<>(collection);
    

    Examples:

    filter_none

    edit

    play_arrow

    brightness_4

    // 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]
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值