A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Write ArrayList as Generic type

To create a list as arraylist as generic type, create object as below:

ArrayList<String> ll = new ArrayList<String>();

or, we can write as below:

ArrayList<Integer> ll = new ArrayList<Integer>();


Program:

import java.util.ArrayList;
public class Gcoll
{
    public static void main(String args[])
    {
        ArrayList<String> ll = new ArrayList<String>();
       
        ll.add("Heeera");
        ll.add("Navneet");
        ll.add("Babu");
        ll.add("kaur");
        //generic type of list is String
        //ll.add(10);
       
        System.out.println(ll);
        ll.remove(3);
        System.out.println(ll);    
    }

}


sample output:


[Heeera, Navneet, Babu, kaur]
[Heeera, Navneet, Babu]

LEARN TUTORIALS

.

.