Java Online Compiler

							
							

ArrayList :
ArrayList is a growable array and which allows dynamic opreations like insertion,deletion,searching,sorting etc..
ArrayList always accepts duplicate elements, memory can be allocated by creating an object as shown below.
ArrayList<Integer> al=new ArrayList<Integer>();  
                                        
Initially memory is allocated for elements later that can be growable to any number of elements
ArrayList class contains various methods to perform various dynamic operations on the elements.


ArrayList Add Method:
Array List
Which can be used to either add or insert the element.
Syntax:
al.add(element);
Which can be used to add the an element at the endof arraylist.


import java.util.*;
class ArrayListAdd
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);

System.out.println("The List Items Are "+al);
}
}

class Main
{
public static void main(String aargs[])
{
ArrayListAdd d=new ArrayListAdd();
d.list();
}
}

ArrayList AddIndex Method:
Which can be used to insert a new element at the given index value.
syntax:
al.add(index,element);

import java.util.*;
class ArrayListAddIndex
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
al.add(2,2000);//it will insert 2 index element
System.out.println("The List Items Are "+al);
}
}

class Main
{
public static void main(String aargs[])
{
ArrayListAddIndex d=new ArrayListAddIndex();
d.list();
}
}

ArrayList AddAll Method:
Which can be used to add all elements of one arraylist to another arraylist.
syntax:
al2.addAll(al1);
Elements of al1 objects will be added to al2 object.


import java.util.*;
class ArrayListAddAll
{
void list()
{
ArrayList<Integer> al1=new ArrayList<Integer>();
al1.add(20);
al1.add(30);
al1.add(40);
al1.add(10);
al1.add(70);

System.out.println("The List Items Are "+al1);
ArrayList<Integer> al2=new ArrayList<Integer>();
al2.addAll(al1);
System.out.println("The List Items Are "+al2);


}
}

class Main
{
public static void main(String aargs[])
{
ArrayListAddAll d=new ArrayListAddAll();
d.list();
}
}

ArrayList Clear Method :
Which can be used to remove all the elements from an arraylist. syntax:
al.clear();
                                    


import java.util.*;
class ArrayListClear
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);

System.out.println("The List Items Are "+al);
al.clear();
System.out.println("After Clear The List Items Are "+al);
}
}

class Main
{
public static void main(String aargs[])
{
ArrayListClear d=new ArrayListClear();
d.list();
}
}

Array ListClone Method
Which can be used to get the get cloning object or duplicate object from the existing array list object. syntax:
ArrayList<Integer> al2;
al2=(ArrayList)al.clone();
                                    
Duplicate object is created for 'al1' for whose reference is assigned to al2.


  • Difference between clone and addAll methods?
  • Close method will create duplicate memory from the existing memory. Where as addAll elements one existing object to another existing object.
                      

import java.util.*;
class ArrayListClone
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);

System.out.println("The List Items Are "+al);
ArrayList<Integer> al2;
al2=(ArrayList)al.clone();

System.out.println("After Cloning The List Items Are "+al2);
}
}

class Main
{
public static void main(String aargs[])
{
ArrayListClone d=new ArrayListClone();
d.list();
}
}

ArrayList Contains Method :
It is a boolean method,it return true if the given element existing in the list otherwise returns false.
al.contains(element);


import java.util.*;
class ArrayListContains
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);

System.out.println("The List Items Are "+al);
System.out.println(al.contains(20));
}
}

class Main
{
public static void main(String aargs[])
{
ArrayListContains d=new ArrayListContains();
d.list();
}
}

ArrayList Get Method
It is used to find element based on index value.
al.get(indexvalue);

import java.util.*;
class ArrayListGet
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);

System.out.println("The List Items Are "+al);
System.out.println(al.get(1));
}
}

class Main
{
public static void main(String aargs[])
{
ArrayListGet d=new ArrayListGet();
d.list();
}
}

ArrayList IndexOf Method
Which can be used to find the index value based on the element or object.
al.indexOf(element);


import java.util.*;
class ArrayListIndexOf
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);

System.out.println("The List Items Are "+al);
System.out.println(al.indexOf(100));
}
}

class Main
{
public static void main(String aargs[])
{
ArrayListIndexOf d=new ArrayListIndexOf();
d.list();
}
}

ArrayList IsEmpty Method
Which can be used to it return true the list is empty otherwise it returns false.
al.isEmpty();


import java.util.*;
class ArrayListIsEmpty
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);

System.out.println("The List Items Are "+al);

System.out.println(al.isEmpty());
}
}

class Main
{
public static void main(String aargs[])
{
ArrayListIsEmpty d=new ArrayListIsEmpty();
d.list();
}
}

ArrayList Remove Method
Which can be used to remove a specific element based on index value.
al.remove(indexvalue);    
It can be removed not only based on index value but also it is possible throw element or value.
                                
import java.util.*;
class ArrayListRemove
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);

System.out.println("The List Items Are "+al);
al.remove(2);
System.out.println("After Remove The List Items Are "+al);
}
}

class Main
{
public static void main(String aargs[])
{
ArrayListRemove d=new ArrayListRemove();
d.list();
}
}

ArrayList Set Method
Which can be used to replace a new element with old element based on the index value.
al.set(indexvalue,element);    
import java.util.*;
class ArrayListSet
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);

System.out.println("The List Items Are "+al);
al.set(1,1000);
System.out.println("After Set The List Items Are "+al);
}
}

class Main
{
public static void main(String aargs[])
{
ArrayListSet d=new ArrayListSet();
d.list();
}
}

ArrayList Size Method:
Which can be used to find the size of array list(number of elements of an arraylist)
al.size();


import java.util.*;
class ArrayListSize
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);

System.out.println("The List Items Are "+al);

System.out.println(al.size());
}
}

class Main
{
public static void main(String aargs[])
{
ArrayListSize d=new ArrayListSize();
d.list();
}
}


ArrayList ToArray Method:
It returns any array containing all the elements of arraylist so that can be use the element of array but no manifulation can be done.
al.toArray();    

import java.util.*;
class ArrayListToArray
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);

System.out.println("The List Items Are "+al);

System.out.println(al.toArray());
}
}

class Main
{
public static void main(String aargs[])
{
ArrayListToArray d=new ArrayListToArray();
d.list();
}
}

Note:
For all the list intrface implemented classes 50% of current location on increased once default memory locations are filled with the element.
Example: Initially for an array list 10 momory locations are created. After storing 10th element 5 memory location will be added(10/2). after storing 15th element 7 memory locations will be added(15/2 or (10+5)/2).
An Arraylist class object is not synchronized that means multiple threads can work simultanusly on the same resource.