Here we are going to create custom ArrayList ,our own ArrayList Class . Let’s first check out basic methods of ArrayList class ,so that we can provide same functionality in our custom ArrayList class.
Basic methods required to create custom ArrayList are :
1) public void add(E element): This method will add objects (element) in ArrayList.
2) public E get(int index) : This method will return element at specific index
3) public Object remove(int index) : This method will remove element at specified index private 4)
4) public void ensureCapacity(): this method will increase capacity of ArrayList by increases its size to double
Program to create custom ArrayList class.
Program to create custom ArrayList 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 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 |
package com.j2eereference; import java.util.Arrays; class Student { private String stuId; private String name; public Student(String id, String name) { // constructor this.stuId = id; this.name = name; } @Override public String toString() { return "Student[student id is : "+ stuId + " and name is " + name + "] "; } } class ArrayListCustom<E> { private static final int INITIAL_CAPACITY = 15; private Object elementArray[]={}; private int size = 0; public ArrayListCustom() { elementArray = new Object[INITIAL_CAPACITY]; } public E get(int index) { if ( index <0 || index>= size) { throw new IndexOutOfBoundsException("Index out of bound exception. Please provide valid index"); } return (E) elementArray[index]; } public void add(E e) { if (size == elementArray.length) { ensureCapacity(); } elementArray[size++] = e; } public Object remove(int index) { if ( index <0 || index>= size) { throw new IndexOutOfBoundsException("Index out of bound exception. Please provide valid index"); } Object removedElement=elementArray[index]; for(int i=index;i<size - 1;i++){ elementArray[i]=elementArray[i+1]; } size--; return removedElement; } private void ensureCapacity() { int newIncreasedCapacity = elementArray.length * 2; elementArray = Arrays.copyOf(elementArray, newIncreasedCapacity); } public void display() { System.out.print("Students present in the ArrayList : "); for(int i=0;i<size;i++){ System.out.print(elementArray[i]+" "); } } } public class CustomArrayList { public static void main(String...a) { ArrayListCustom<Student> customList = new ArrayListCustom<Student>(); System.out.println("Adding student in custom ArrayList"); customList.add(new Student("101", "Shobhna")); customList.add(new Student("102", "Shilpi")); customList.add(new Student("105", "Upasana")); customList.add(new Student("108", "Gurpal")); customList.add(new Student("110", "Mohit")); customList.add(new Student("112", "Harpreet")); customList.add(new Student("111", "Raghu")); customList.add(new Student("115", "Krishna")); customList.display(); System.out.println("\n Student at index "+6+" = "+customList.get(6)); System.out.println("Student removed from index "+4+" = "+customList.remove(4)); customList.display(); customList.remove(10); } } |
OutPut:
Adding student in custom ArrayList
Students present in the ArrayList : Student[student id is : 101 and name is Shobhna] Student[student id is : 102 and name is Shilpi] Student[student id is : 105 and name is Upasana] Student[student id is : 108 and name is Gurpal] Student[student id is : 110 and name is Mohit] Student[student id is : 112 and name is Harpreet] Student[student id is : 111 and name is Raghu] Student[student id is : 115 and name is Krishna]
Student at index 6 = Student[student id is : 111 and name is Raghu]
Student removed from index 4 = Student[student id is : 110 and name is Mohit]
Students present in the ArrayList : Student[student id is : 101 and name is Shobhna] Student[student id is : 102 and name is Shilpi] Student[student id is : 105 and name is Upasana] Student[student id is : 108 and name is Gurpal] Student[student id is : 112 and name is Harpreet] Student[student id is : 111 and name is Raghu] Student[student id is : 115 and name is Krishna] Exception in thread “main” java.lang.IndexOutOfBoundsException: Index out of bound exception. Please provide valid index
at com.j2eereference.ArrayListCustom.remove(CustomArrayList.java:48)
at com.j2eereference.CustomArrayList.main(CustomArrayList.java:96)
We are getting IndexOutOfBoundException at the end of the output as we are removing element at index 10 but our custom ArrayList contains only 8 elements.