Initialize the ArrayList in Java

Java collection framework was non-generic before JDK 1.5. Since 1.5, it is generic. Java generic collections allows you to have one type of object in collection i.e. you can create collection for String type or int type etc.

ArrayList alist=new ArrayList();//creating old non-generic arraylist
ArrayList<String> alist=new ArrayList<String>();//creating new generic arraylist

There are many ways to initialize the ArrayList as given below. Let’s learn them one by one.

1) Initialize with a particular type in angular braces:

Syntax: 

ArrayList<T> obj = new ArrayList<T>();
	   obj.add("Object obj1");
	   obj.add("Object obj2");
	   obj.add("Object obj3");
           //You can add more objects

Example:

import java.util.ArrayList;

public class ArrayLIstEx {

	
	public static void main(String args[]){
		ArrayList<String> Banklist = new ArrayList<String>();	
		Banklist.add("ICICI");
		Banklist.add("HDFC");
		Banklist.add("SBI");

		System.out.println("All Bank Names: "+Banklist);
	}
}
All Bank Names: [ICICI, HDFC, SBI]

2) Initialization using Arrays.asList:

Syntax:

ArrayList<Type> obj = new ArrayList<Type>(
        Arrays.asList(Object obj1, Object obj2, Object obj3, ....))

Example:

import java.util.ArrayList;
import java.util.Arrays;

public class ArrayLIstEx {

	
	public static void main(String args[]){
		
		ArrayList<String> Banklist = new ArrayList<String>(	
		Arrays.asList("ICICI", "HDFC", "SBI"));
	
		System.out.println("All Bank Names: "+Banklist);
	}
}
All Bank Names: [ICICI, HDFC, SBI]

3) Initialize using anonymous inner class method:

Syntax:

ArrayList<T> obj = new ArrayList<T>(){{
		   add(Object obj1);
		   add(Object obj2);
		   add(Object obj3);
                   //You can add more objects here
                  
		   }};

Example:

import java.util.ArrayList;

public class ArrayLIstEx {
	
	public static void main(String args[]){
		
		ArrayList<String> Banklist = new ArrayList<String>(){
			{
			add("ICICI");
			add("HDFC");
			add("SBI");
			}
		};
	
		System.out.println("All Bank Names: "+Banklist);
	}
}
All Bank Names: [ICICI, HDFC, SBI]

4) Using Collections.ncopies :

This method can be used when we need to initialize the same values for all of its elements.

Arguments:
Count: 
Number of elements
Element: The item value which needs to be stored

Syntax:

ArrayList<T> obj = new ArrayList<T>(Collections.nCopies(count, element));

Example:

import java.util.ArrayList;
import java.util.Collections;

public class ArrayLIstEx {
	
	public static void main(String args[]){
		
		ArrayList<Integer> itemlist = new ArrayList<Integer>(Collections.nCopies(10, 2));
	
		System.out.println("All Items : "+itemlist);
	}
}
All Bank Names: [ICICI, HDFC, SBI]
Questions/Suggestions
Have any question or suggestion for us?Please feel free to post in Q&A Forum

 

Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

1 Response

  1. Anil says:

    What exactly does Collection. ncopies do ?is the output correct for this example?
    public class ArrayLIstEx {

    public static void main(String args[]){

    ArrayList itemlist = new ArrayList(Collections.nCopies(10, 2));

    System.out.println(“All Items : “+itemlist);
    }
    }

    this is what i am asking about : All Bank Names: [ICICI, HDFC, SBI]