|
| 1 | +package listExamples; |
| 2 | +import java.util.ArrayList; |
| 3 | +import java.util.Iterator; |
| 4 | +import java.util.List; |
| 5 | +import java.util.ListIterator; |
| 6 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 7 | +public class ArrayListExample { |
| 8 | + /** List(I) is the Child of Collection(I). |
| 9 | + * ArrayList (C) is one of the classes provides implementation for the List(I). |
| 10 | + * In list duplicate values are allowed and the insertion order is maintained. |
| 11 | + * The underlying DS is resizeable Array or Growable Array. We can insert Heterogeneous objects as well. |
| 12 | + * NOTE: All the collections can store Heterogeneous objects can be stored except TREE SET and TREE MAP. |
| 13 | + * ArraList and vector implements RandomAccess, Serializable and Cloneable Interfaces |
| 14 | + * Synchronized-> No |
| 15 | + * Thread safe-> NO |
| 16 | + */ |
| 17 | + public void arrayListExample(){ |
| 18 | + |
| 19 | + //add() used to add the specified element at the end of the List |
| 20 | + List<String> arrayList= new ArrayList<String>(); |
| 21 | + arrayList.add("Audi"); |
| 22 | + arrayList.add("Benz"); |
| 23 | + arrayList.add("Bugatti"); |
| 24 | + arrayList.add("Aston martin"); |
| 25 | + |
| 26 | + System.out.println(arrayList); |
| 27 | + |
| 28 | + //add(int index, Object element) method will add at specified index position |
| 29 | + arrayList.add(0,"Mustang"); |
| 30 | + System.out.println(arrayList); |
| 31 | + |
| 32 | + //to check the indexof any element |
| 33 | + System.out.println("Index position of Audi is :"+arrayList.indexOf("Audi")); |
| 34 | + //-1 is returned if the element is not there |
| 35 | + System.out.println("Index position of Ambassador is :"+arrayList.indexOf("Ambassador")); |
| 36 | + |
| 37 | + //List allows duplicate elements |
| 38 | + arrayList.add("Mustang"); |
| 39 | + System.out.println(arrayList); |
| 40 | + |
| 41 | + //indexof always returns the first occurrence |
| 42 | + System.out.println("Index position of Mustang is :"+arrayList.indexOf("Mustang")); |
| 43 | + //last index of is used to check the last occurence position |
| 44 | + System.out.println("Last Index position of Mustang is :"+arrayList.lastIndexOf("Mustang")); |
| 45 | + |
| 46 | + // add a list to another list |
| 47 | + List<String> anotherList=new ArrayList<String>(); |
| 48 | + anotherList.addAll(arrayList); |
| 49 | + System.out.println("New List copied :"+ anotherList); |
| 50 | + |
| 51 | + //clear to delete all the elements |
| 52 | + anotherList.clear(); |
| 53 | + System.out.println("List after clearing "+anotherList); |
| 54 | + |
| 55 | + //we can even insert null |
| 56 | + anotherList.add(null); |
| 57 | + System.out.println("After adding null "+anotherList); |
| 58 | + anotherList.add("mango"); |
| 59 | + anotherList.add("Banana"); |
| 60 | + //adding list to a list at specified position. |
| 61 | + anotherList.addAll(0,arrayList); |
| 62 | + System.out.println("New list after adding the old list at 0th position :"+anotherList); |
| 63 | + |
| 64 | + //set() is used to update the element based on index |
| 65 | + anotherList.set(6, "Tata"); |
| 66 | + anotherList.set(7,"Civic"); |
| 67 | + System.out.println("List after updating the last two elements : "+anotherList); |
| 68 | + |
| 69 | + //remove(int position) removes the value at the specified position |
| 70 | + anotherList.remove(6); |
| 71 | + System.out.println("After removing :"+anotherList); |
| 72 | + |
| 73 | + //remove using object value |
| 74 | + anotherList.remove("Civic"); |
| 75 | + System.out.println("After removing Civic: "+ anotherList); |
| 76 | + |
| 77 | + /*get an element based on index position. |
| 78 | + If the index is not there we will get index out of bound exception*/ |
| 79 | + System.out.println("Element at 5th Position "+anotherList.get(5)); |
| 80 | + |
| 81 | + //isEmpty() method to check the list is empty or not |
| 82 | + System.out.println("This list is empty. True or False? "+ anotherList.isEmpty()); |
| 83 | + System.out.println(anotherList); |
| 84 | + |
| 85 | + //get all the elements in the list using for loop |
| 86 | + for(int size=0; size<anotherList.size();size++){ |
| 87 | + System.out.println("element at "+size+"th position " +anotherList.get(size)); |
| 88 | + } |
| 89 | + |
| 90 | + //fetch using advanced for loop |
| 91 | + for (String string : anotherList) { |
| 92 | + System.out.println("List elements "+string); |
| 93 | + } |
| 94 | + |
| 95 | + //forward traversing using ListIterator |
| 96 | + ListIterator<String> listIterator= anotherList.listIterator(); |
| 97 | + while(listIterator.hasNext()){ |
| 98 | + System.out.println("Forward Iteration : "+listIterator.next()); |
| 99 | + } |
| 100 | + |
| 101 | + // reverse Iteration using ListIterator |
| 102 | + while(listIterator.hasPrevious()){ |
| 103 | + System.out.println("Reverse Iteration : "+listIterator.previous()); |
| 104 | + } |
| 105 | + |
| 106 | + /*Iteration with Iterator (NOTE: Not ListIterator) |
| 107 | + Iterator can only traverse forward but not on reverse. |
| 108 | + Hence we are using ListIterator for better usages.*/ |
| 109 | + |
| 110 | + Iterator< String> iterator=anotherList.iterator(); |
| 111 | + while(iterator.hasNext()){ |
| 112 | + System.out.println("Forward Only:"+iterator.next()); |
| 113 | + } |
| 114 | + |
| 115 | + /* ArrayList is non-synchronized.It should not be used in multi-threaded |
| 116 | + * environment without explicit synchronization.Hence, |
| 117 | + * adding an element to the list when traversing through it |
| 118 | + * will give concurrent modification exception. |
| 119 | + * This is happening because when a thread is trying to read the elements |
| 120 | + * from the list another thread is trying to add or remove an element from the same. |
| 121 | + */ |
| 122 | + try{ |
| 123 | + for (String string : anotherList) { |
| 124 | + System.out.println("Reading the list values"+ string); |
| 125 | + anotherList.add("Mustang"); |
| 126 | + } |
| 127 | + } |
| 128 | + catch(Exception e){ |
| 129 | + |
| 130 | + System.out.println("Dude! You are trying to modify a list while a read operation is happening"); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /* CopyOnWriteArrayList allows us to modify |
| 135 | + * the list while reading it |
| 136 | + */ |
| 137 | + public void syncArrayListExample(){ |
| 138 | + |
| 139 | + CopyOnWriteArrayList<String> syncal = new CopyOnWriteArrayList<String>(); |
| 140 | + |
| 141 | + //Adding elements to synchronized ArrayList |
| 142 | + syncal.add("Pen"); |
| 143 | + syncal.add("NoteBook"); |
| 144 | + syncal.add("Ink"); |
| 145 | + System.out.println("Iterating synchronized ArrayList:"); |
| 146 | + |
| 147 | + Iterator<String> syncIterator = syncal.iterator(); |
| 148 | + while (syncIterator.hasNext()){ |
| 149 | + System.out.println(syncIterator.next()); |
| 150 | + syncal.add("Eraser"); |
| 151 | + syncal.remove("Eraser"); |
| 152 | + |
| 153 | + |
| 154 | + } |
| 155 | + System.out.println("Final List: "+syncal); |
| 156 | + } |
| 157 | + |
| 158 | + public static void main(String[] args) { |
| 159 | + // TODO Auto-generated method stub |
| 160 | + ArrayListExample example= new ArrayListExample(); |
| 161 | + example.arrayListExample(); |
| 162 | + //example.syncArrayListExample(); |
| 163 | + |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments