Tuesday, 21 June 2016

Java ArrayList Examples For Programmers

ArrayList Example in Java
In this Java ArrayList Example we will see how to add elements in ArrayList, how to remove elements from ArrayList, ArrayList contains Example and several other ArrayList functions which we use daily. ArrayList is one of the most popular class from Java Collection framework along with HashSet and HashMap and a good understanding of ArrayList class and methods is imperative for Java developers. ArrayList is an implementation ofList Collection which is ordered and allow duplicates. ArrayList is alos index based and provides constant time performance for common methods e.g. get().Apart from very popular among Java programmers, ArrayList is also a very popular interview topic. Questions like Difference between Vector and ArrayList and LinkedList vs ArrayList is hugely popular on various Java interview specially with 2 to 3 years of experience. Along with Vector this is one of the first collection class many Java programmer use. By the way e have already seen some ArrayList tutorial e.g. ArrayList sorting example,  converting Array to ArrayList looping through ArrayList which is good to understand ArrayList in Java.

Java ArrayList Examples
Java ArrayList Example contains empty size removeIn this section we will see actual code example of various ArrayList functionality e.g. add, remove, contains,clear, size, isEmpty etc.


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

/**
 *
 * Java ArrayList Examples - list of frequently used examples in ArrayList e.g. adding 
 * elements, removing elements, contains examples etc
 * @author
 */

public class ArrayListTest {

    public static void main(String args[]) {
      
        //How to create ArrayList in Java - example
        ArrayList<String> list = new ArrayList<String>();
      
        //Java ArrayList add Examples
        list.add("Apple");
        list.add("Google");
        list.add("Samsung");
        list.add("Microsoft");
    
        //Java ArrayList contains Example, equals method is used to check if
        //ArrayList contains an object or not
        System.out.println("Does list contains Apple :" + list.contains("Apple"));
        System.out.println("Does list contains Verizon :" + list.contains("Verizon"));
      
        //Java ArrayList Example - size
        System.out.println("Size of ArrayList is : " + list.size());
      
        //Java ArrayList Example - replacing an object
        System.out.println("list before updating : " + list);
        list.set(3, "Bank of America");
        System.out.println("list after update : " + list);
      
        //Java ArrayList Example - checking if ArrayList is empty
        System.out.println("Does this ArrayList is empty : " + list.isEmpty());
      
        //Java ArrayList Example - removing an Object from ArrayList
        System.out.println("ArrayList before removing element : " + list);
        list.remove(3); //removing fourth object in ArrayList
        System.out.println("ArrayList after removing element : " + list);
      
       //Java ArrayList Example - finding index of Object in List
        System.out.println("What is index of Apple in this list : " + list.indexOf("Apple"));
      
        //Java ArrayList Example - converting List to Array
        String[] array = list.toArray(new String[]{});
        System.out.println("Array from ArrayList : " + Arrays.toString(array));
      
        //Java ArrayList Example : removing all elements from ArrayList
        list.clear();
        System.out.println("Size of ArrayList after clear : " + list.size());
    } 
  
}

Output:
Does list contains Apple :true
Does list contains Verizon :false
Size of ArrayList is : 4
list before updating : [Apple, Google, Samsung, Microsoft]
list after update : [Apple, Google, Samsung, Bank of America]
Does this ArrayList is empty : false
ArrayList before removing element : [Apple, Google, Samsung, Bank of America]
ArrayList after removing element : [Apple, Google, Samsung]
What is index of Apple in this list : 0
Array from ArrayList : [Apple, Google, Samsung]
Size of ArrayList after clear : 0

These were some frequently used examples of ArrayList in Java. We have seen ArrayList contains example which used equals method to check if an Object is present in ArrayList or not. We have also see how to add, remove and modify contents of ArrayList etc.

Other Java Collection tutorial and Interview Questions



No comments:

Post a Comment