21 July, 2012

ArrayList in Java

My post today is about java.util.ArrayList in java and how to perform popular operation on ArrayList.

When writing this post I find the best thing is to write java class for doing operations  and commenting the operation in class.


 import java.util.ArrayList;   
  import java.util.Arrays;   
  import java.util.Collections;   
  import java.util.Iterator;   
  import java.util.List;   
  import java.util.ListIterator;   
  public class ArrayListDemo {   
   public static void arrayListOperations() {   
   
    
   
   //Create new two objects of ArrayList   
    System.out.println("===============================================================");   
    System.out.println("Create new two objects of ArrayList");   
    ArrayList list1 = new ArrayList();   
    ArrayList list2 = new ArrayList();   
   
    
   
  // Adding to list1 object 10 elements using loop   
    System.out.println("===============================================================");   
    System.out.println("Adding to list1 object 10 elements using loop");   
    for (int i = 0; i <= 10; i++) {   
     list1.add(i);   
    }   
   
    
   
  //Adding to list1 object String Objects   
    System.out.println("===============================================================");   
    System.out.println("Adding to list1 object String Objects");   
    list1.add("Mahmoud");   
    list1.add("El-Sayed");   
    //Retrieve every elements stored in ArrayList using Iterator   
    System.out.println("===============================================================");   
    System.out.println("Retrieve every elements stored in ArrayList using Iterator");   
    Iterator iterator = list1.iterator();   
    while (iterator.hasNext()) {   
     System.out.println(iterator.next().toString());   
    }  
   
    
   
  //Retrieve every elements stored in ArrayList using ListIterator   
    System.out.println("===============================================================");   
    System.out.println("Retrieve every elements stored in ArrayList using ListIterator");   
    ListIterator listIterator = list1.listIterator();   
    while (listIterator.hasNext()) {   
     System.out.println(listIterator.next().toString() + " PreviousIndex" + listIterator.previousIndex() +   
          " NextIndex" + listIterator.nextIndex());   
    }  
   
    
   
  //Retrieve every elements stored in ArrayList using indexing   
    System.out.println("===============================================================");   
    System.out.println("Retrieve every elements stored in ArrayList using indexing");   
    for (int i = 0; i < list1.size(); i++) {   
     System.out.println(list1.get(i).toString());   
    }  
   
    
   
  //Search about particular object and get its index in ArrayList   
    System.out.println("===============================================================");   
    System.out.println("Search about particular object and get its index in ArrayList");   
    int index = list1.indexOf("Mahmoud");   
    System.out.println("Mahmoud exists in index " + index);  
   
    
   
  //search about last index of particular object   
    System.out.println("===============================================================");   
    System.out.println("search about last index of particular object");   
    index = list1.lastIndexOf("Mahmoud");   
    System.out.println("Last index of Mahmoud is " + index);  
   
    
   
  //Create sub list from index 10 to end of the array list   
    System.out.println("===============================================================");   
    System.out.println("Create sub list from index 11 to end of the array list");   
    List subList = list1.subList(11, list1.size());   
    System.out.println("The sublist is " + subList);  
   
    
   
  //Sort the created sub list   
    System.out.println("===============================================================");   
    System.out.println("Sort the created sub list");   
    Collections.sort(subList);   
    System.out.println("Sublist after sorting " + subList);  
   
    
   
  //Reverse the created sublist   
    System.out.println("===============================================================");   
    System.out.println("Reverse the created sublist");   
    Collections.reverse(subList);   
    System.out.println("sublist after reversing " + subList);  
   
    
   
  //Check if array list and list is empty   
    System.out.println("===============================================================");   
    System.out.println("Check if array list and list is empty ");   
    System.out.println("list1 is empty " + list1.isEmpty());   
    System.out.println("list2 is empty " + list2.isEmpty());   
    System.out.println("subList is empty " + subList.isEmpty());  
   
    
   
  //check if list1 is equal to list2   
    System.out.println("===============================================================");   
    System.out.println("check if list1 is equal to list2");   
    System.out.println("Is list1 is equal to list2? " + list1.equals(list2));   
   
    
   
  //Convert List to array and print array   
    System.out.println("===============================================================");   
    System.out.println("Convert List to array and print array");   
    Object objs[] = list1.toArray();   
    System.out.println("Print list after converting to Array " + Arrays.toString(objs));   
   
    
   
  //Remove whole elements from Array List   
    System.out.println("===============================================================");   
    System.out.println("Remove whole elements from Array List");   
    list1.clear();   
    System.out.println("Elements in list1 after clearing " + list1);   
   }   
   
    
   
  public static void main(String[] args) {   
    ArrayListDemo.arrayListOperations();   
   }   
  }   
   




The output of running this java class is
 ===============================================================  
 Create new two objects of ArrayList  
 ===============================================================  
 Adding to list1 object 10 elements using loop  
 ===============================================================  
 Adding to list1 object String Objects  
 ===============================================================  
 Retrieve every elements stored in ArrayList using Iterator  
 0  
 1  
 2  
 3  
 4  
 5  
 6  
 7  
 8  
 9  
 10  
 Mahmoud  
 El-Sayed  
 ===============================================================  
 Retrieve every elements stored in ArrayList using ListIterator  
 0 PreviousIndex0 NextIndex1  
 1 PreviousIndex1 NextIndex2  
 2 PreviousIndex2 NextIndex3  
 3 PreviousIndex3 NextIndex4  
 4 PreviousIndex4 NextIndex5  
 5 PreviousIndex5 NextIndex6  
 6 PreviousIndex6 NextIndex7  
 7 PreviousIndex7 NextIndex8  
 8 PreviousIndex8 NextIndex9  
 9 PreviousIndex9 NextIndex10  
 10 PreviousIndex10 NextIndex11  
 Mahmoud PreviousIndex11 NextIndex12  
 El-Sayed PreviousIndex12 NextIndex13  
 ===============================================================  
 Retrieve every elements stored in ArrayList using indexing  
 0  
 1  
 2  
 3  
 4  
 5  
 6  
 7  
 8  
 9  
 10  
 Mahmoud  
 El-Sayed  
 ===============================================================  
 Search about particular object and get its index in ArrayList  
 Mahmoud exists in index 11  
 ===============================================================  
 search about last index of particular object  
 Last index of Mahmoud is 11  
 ===============================================================  
 Create sub list from index 11 to end of the array list  
 The sublist is [Mahmoud, El-Sayed]  
 ===============================================================  
 Sort the created sub list  
 Sublist after sorting [El-Sayed, Mahmoud]  
 ===============================================================  
 Reverse the created sublist  
 sublist after reversing [Mahmoud, El-Sayed]  
 ===============================================================  
 Check if array list and list is empty   
 list1 is empty false  
 list2 is empty true  
 subList is empty false  
 ===============================================================  
 check if list1 is equal to list2  
 Is list1 is equal to list2? false  
 ===============================================================  
 Convert List to array and print array  
 Print list after converting to Array [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, Mahmoud, El-Sayed]  
 ===============================================================  
 Remove whole elements from Array List  
 Elements in list1 after clearing []  


Thanks

1 comment:

ADF : Scope Variables

Oracle ADF uses many variables and each variable has a scope. There are five scopes in ADF (Application, Request, Session, View and PageFl...