Java Tutorial

Array List

java.util.ArrayList:

ArrayList is also concrete sub-class of collection framework classes whose object allows us to organize the data either in similar type or in different type.

Creating ArrayList is nothing but creating an object of ArrayList class.

ArrayList API:

ArrayList (); 
ArrayList (int size);

Advantages of ArrayList over LinkedList:

  1. No additional memory space is required for data of ArrayList.
  2. Retrieval time is quite faster.
  3. Performance is high. Since, there is no memory space is required for maintaining address of data of ArrayList.

ArrayList

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
 
public class ArrayListDemo {
    public static void main(String[] args) {
        List<String> country = new ArrayList();
 
        country.add("India");
        country.add("Pakistan");
        country.add("China");
        country.add("SriLanka");
        country.add("Australia");
        country.add("England");
        country.add("Japan");
        country.add("Bangladesh");
        country.add("USA");
 
        //Iterate ArrayList using for loop
        System.out.println("Using for loop");
        for (int i = 0; i < country.size(); i++) {
            System.out.println(country.get(i));
        }
 
        //Iterate ArrayList using advanced for loop
        System.out.println("Using Advanced for loop");
        for (String name : country) {
            System.out.println(name);
        }
 
        //Iterate ArrayList using Iterator
        System.out.println("Using Iterator");
        Iterator iterator = country.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
 
        //Iterate ArrayList using wihle loop
        System.out.println("Using while  loop");
        int index = 0 ;
        while (country.size()> index) {
            System.out.println(country.get(index));
            index++ ;
        }
        
        //Add element on particular index
        country.add(2, "Koria");
        System.out.println("After adding new element on 1 index");
        Iterator iteratorNew = country.iterator();
        while (iteratorNew.hasNext()) {
            System.out.println(iteratorNew.next());
        }
        
        //Add the one ArrayList data to another ArrayList
        List<String> newCountry = new ArrayList();
        newCountry.add("SouthAfrica");
        country.addAll(newCountry);
        System.out.println("After adding new ArrayList to country List");
        Iterator iteratorNewCountry = country.iterator();
        while (iteratorNewCountry.hasNext()) {
            System.out.println(iteratorNewCountry.next());
        }
        
        //Clear all the element from ArrayList
        newCountry.clear();
        System.out.println("After clearing newCountry List");
        Iterator iteratorAfterClear = newCountry.iterator();
        while (iteratorAfterClear.hasNext()) {
            System.out.println(iteratorAfterClear.next());
        }
        
       //find the element in ArryList using contains() method
        if(country.contains("India")){
            System.out.println("The ArrayList having country India");
        }
        
        //find the index of a element in ArrayList
        int indexOfIndia = country.indexOf("India");
        if(indexOfIndia != -1){
            System.out.println("The index of India is:"+indexOfIndia);
        }
        
        //test the ArrayList whether it is empty or not?
        if(!country.isEmpty()){
            System.out.println("Country list is not empty.");
        }
        
        //Sort the ArrayList
        Collections.sort(country);
        System.out.println("After sorted the order");
        Iterator iteratorSort = country.iterator();
        while (iteratorSort.hasNext()) {
            System.out.println(iteratorSort.next());
        }
        
        //Remove the element form Array
        //Based on index
        country.remove(1);
        
        //Based on element
        country.remove("USA");
        
        //remove the element by Collection 
        List deletedList = new ArrayList();
        deletedList.add("Pakistan");
        country.removeAll(deletedList);
        
        System.out.println("After removing the elements");
        Iterator iteratorRemove = country.iterator();
        while (iteratorRemove.hasNext()) {
            System.out.println(iteratorRemove.next());
        }
        
        //Retains only the elements in this list that are contained in the specified collection
        List retain = new ArrayList();
        retain.add("Koria");
        country.retainAll(retain);
        System.out.println("After retaining the elements");
        Iterator iteratorRetain = country.iterator();
        while (iteratorRetain.hasNext()) {
            System.out.println(iteratorRetain.next());
        }
        
    }
}

Output:

  1. Using for loop
  2. India
  3. Pakistan
  4. China
  5. SriLanka
  6. Australia
  7. England
  8. Japan
  9. Bangladesh
  10. USA
  11.  
  12. Using Advanced for loop
  13. India
  14. Pakistan
  15. China
  16. SriLanka
  17. Australia
  18. England
  19. Japan
  20. Bangladesh
  21. USA
  22.  
  23. Using Iterator
  24. India
  25. Pakistan
  26. China
  27. SriLanka
  28. Australia
  29. England
  30. Japan
  31. Bangladesh
  32. USA
  33.  
  34. Using while loop
  35. India
  36. Pakistan
  37. China
  38. SriLanka
  39. Australia
  40. England
  41. Japan
  42. Bangladesh
  43. USA
  44.  
  45. After adding new element on 1 index
  46. India
  47. Pakistan
  48. Koria
  49. China
  50. SriLanka
  51. Australia
  52. England
  53. Japan
  54. Bangladesh
  55. USA
  56.  
  57. After adding new ArrayList to country List
  58. India
  59. Pakistan
  60. Koria
  61. China
  62. SriLanka
  63. Australia
  64. England
  65. Japan
  66. Bangladesh
  67. USA
  68. SouthAfrica
  69.  
  70. After clearing newCountry List The ArrayList having country India The index of India is:0 Country list is not empty.
  71.  
  72. After sorted the order
  73. Australia
  74. Bangladesh
  75. China
  76. England
  77. India
  78. Japan
  79. Koria
  80. Pakistan
  81. SouthAfrica
  82. SriLanka
  83. USA
  84.  
  85. After removing the elements
  86. Australia
  87. China
  88. England
  89. India
  90. Japan
  91. Koria
  92. SouthAfrica
  93. SriLanka
  94.  
  95. After retaining the elements
  96. Koria