Java ArrayList: add() and size() methods
KW.java
import java.util.ArrayList;

class KW 
{
    public static void main(String[] args) 
    {
        //ArrayList class implements the List interface
        ArrayList<String> al=new ArrayList<>();
        al.add("Apple");
        al.add("Orange");
        al.add("Mango");
        al.add("Grapes");
        al.add("Cherry");
        al.add("Apple");
        System.out.println(al);
        System.out.println("Size: "+al.size());
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW [Apple, Orange, Mango, Grapes, Cherry, Apple] Size: 6 kodingwindow@kw:~$
Java ArrayList: get() and indexOf() methods
KW.java
import java.util.ArrayList;

class KW 
{
    public static void main(String[] args) 
    {
        //ArrayList class implements the List interface
        ArrayList<String> al=new ArrayList<>();
        al.add("Apple");
        al.add("Orange");
        al.add("Mango");
        al.add("Grapes");
        al.add("Cherry");
        al.add("Apple");
        for(String s:al)
        {
            System.out.println(s);
        }
        System.out.println("\n"+al.get(3));
        System.out.println(al.indexOf("Apple"));
        System.out.println(al.lastIndexOf("Apple"));
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Apple Orange Mango Grapes Cherry Apple Grapes 0 5 kodingwindow@kw:~$
Java ArrayList: subList() and set() methods
KW.java
import java.util.ArrayList;

class KW 
{
    public static void main(String[] args) 
    {
        //ArrayList class implements the List interface
        ArrayList<String> al=new ArrayList<>();
        al.add("Apple");
        al.add("Orange");
        al.add("Mango");
        al.add("Grapes");
        al.add("Cherry");
        al.add("Apple");
        System.out.println(al);
        System.out.println(al.subList(2, 4));
        al.set(0, "Pineapple");
        System.out.println(al);
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW [Apple, Orange, Mango, Grapes, Cherry, Apple] [Mango, Grapes] [Pineapple, Orange, Mango, Grapes, Cherry, Apple] kodingwindow@kw:~$
Java ArrayList: addAll() and containsAll() methods
KW.java
import java.util.ArrayList;

class KW 
{
    public static void main(String[] args) 
    {
        //ArrayList class implements the List interface
        ArrayList<String> al1=new ArrayList<>();
        al1.add("Apple");
        al1.add("Orange");
        al1.add("Mango");
        al1.add("Grapes");
        al1.add("Cherry");
        System.out.println(al1);
        
        ArrayList<String> al2=new ArrayList<>();
        al2.add("Strawberry");
        al2.add("Pineapple");
        System.out.println(al2);
        
        al1.addAll(al2);
        System.out.println(al1);
        System.out.println(al1.containsAll(al2));
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW [Apple, Orange, Mango, Grapes, Cherry] [Strawberry, Pineapple] [Apple, Orange, Mango, Grapes, Cherry, Strawberry, Pineapple] true kodingwindow@kw:~$
Java ArrayList: remove() and removeAll() methods
KW.java
import java.util.ArrayList;

class KW 
{
    public static void main(String[] args) 
    {
        //ArrayList class implements the List interface
        ArrayList<String> al=new ArrayList<>();
        al.add("Apple");
        al.add("Orange");
        al.add("Mango");
        al.add("Grapes");
        al.add("Cherry");
        al.add("Apple");
        System.out.println(al);
        
        //al.remove() //Without arguments its error in ArrayList
        al.remove(2);
        System.out.println(al);
        al.remove("Apple");
        System.out.println(al);
        
        al.removeAll(al); //al.clear();
        if(al.isEmpty())
        {
            System.out.println("List is empty now");
        }
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW [Apple, Orange, Mango, Grapes, Cherry, Apple] [Apple, Orange, Grapes, Cherry, Apple] [Orange, Grapes, Cherry, Apple] List is empty now kodingwindow@kw:~$
Advertisement