Hey there!
Welcome to ClearUrDoubt.com.
In this post, we will look at the Custom implementation of the ArrayList class in Java.
Custom ArrayList Implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
package com.clearurdoubt; import java.util.Arrays; public class MyArrayList { private Object[] arrayObjs; private int idx; // Constructors public MyArrayList() { this(10); } public MyArrayList(int initialcapacity) { arrayObjs = new Object[initialcapacity]; } /** * * @return size of the List */ public int size() { return idx; } /** * * @param index * @return status of the operation */ public boolean add(Object obj) { return add(idx, obj); } /** * * @param index of the new object * @param obj to be added to the List * @return status of the operation */ public boolean add(int index, Object obj) { // check for the index out of bounds condition if(index < 0 || index > idx) { throw new ArrayIndexOutOfBoundsException(); } // increase the size of the list if idx reaches the maximum limit if(idx > arrayObjs.length - 1) { increaseSize(); } if(index < idx) { // Prior to inserting the new element, move all the elements to next positions for(int temp = idx+1; temp > index; temp--) { arrayObjs[temp] = arrayObjs[temp-1]; } // insert the element arrayObjs[index] = obj; // as the new element is added, increment the idx value. idx++; } else { // insert the element at the idx position arrayObjs[idx++] = obj; } return true; } /** * * @param obj * @return status of the operation */ public boolean remove(Object obj) { // Find the index of the obj in the array and call remove(index) method for(int i = 0; i < idx; i++) { if(arrayObjs[i].equals(obj)) { return remove(i); } } return true; } /** * * @param index * @return status of the operation */ public boolean remove(int index) { if(index < 0 || index > idx) { throw new ArrayIndexOutOfBoundsException(); } for(int i = index; i < idx - 1; i++) { arrayObjs[i] = arrayObjs[i+1]; } // as the new element is deleted, decrement the idx value. arrayObjs[idx-1] = null; idx--; return true; } private void increaseSize() { int newcapacity = (arrayObjs.length * 3/2) + 1; arrayObjs = Arrays.copyOf(arrayObjs, newcapacity); } @Override public String toString() { StringBuilder sb = new StringBuilder(); for(int i = 0; i < idx; i++) { sb.append(arrayObjs[i].toString() + ","); } return "[" + sb.toString().substring(0, sb.length() - 1) + "]"; } } |
Let’s see the demo of MyArrayList class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package com.clearurdoubt; public class MyArrayListDemo { public static void main(String[] args) { MyArrayList myAL = new MyArrayList(); // add elements to the List myAL.add("A"); myAL.add("B"); myAL.add("C"); System.out.println(myAL + " @@@ " + myAL.size()); // print elements and size myAL.add(1, "D"); myAL.add(2, "E"); System.out.println(myAL + " @@@ " + myAL.size()); // print elements and size myAL.add(5, "F"); System.out.println(myAL + " @@@ " + myAL.size()); // print elements and size for(int i = 0; i < 10; i++) { myAL.add(i); } System.out.println(myAL + " @@@ " + myAL.size()); // print elements and size // Lets check the remove operations // remove the object myAL.remove("F"); System.out.println("After removing \"F\": "); System.out.println(myAL + " @@@ " + myAL.size()); // print elements and size // remove the object at index myAL.remove(5); System.out.println("After removing element at index 5: "); System.out.println(myAL + " @@@ " + myAL.size()); // print elements and size // remove element which is not in the ArrayList myAL.remove("F"); System.out.println("After removing \"F\": "); System.out.println(myAL + " @@@ " + myAL.size()); // print elements and size // remove last element in the ArrayList myAL.remove(new Integer(9)); System.out.println("After removing \"9\": "); System.out.println(myAL + " @@@ " + myAL.size()); // print elements and size try { myAL.add(40, "Z"); //this will throw an exception } catch(ArrayIndexOutOfBoundsException ex) { System.out.println("ArrayIndexOutOfBoundsException raised."); } } } |
Output:
Happy Learning :).
Please leave a reply in case of any queries.
I’m finding it easy here.. Thanks man!!
Are you conducting any classes for Apache spark,
Please let me know if anything is planned.
I’m looking for good tutor for spark.
Hi.. can you please explain how to remove duplicate custom objects from ArrayList?
Thank you for visiting clearurdoubt.com. Please go through the below link to check out the program to remove duplicates from a List object.
http://clearurdoubt.com/how-to-remove-duplicate-user-defined-objects-from-a-list-collection-object/
Thanks for the timely response..
Easy to understand
Thank you :).