One of the famous interview Question
What is ArrayList? and its implementation with example
What is ArrayList? and its implementation with example
- ArrayList is a class, that uses a dynamic array for storing the elements.
- Arraylist extends AbstractList class and implements List interface.
- ArrayLists are created with an initial size, when this size is exceeded, it gets increased automatically.
- It contains duplicate elements.
- Maintains insertion order.
- Not synchronized.
- Random access because array works at the index basis.
- Manipulation slows because a lot of shifting needs to be occurred if any element is removed from the array list.
- null insertion is possible.
- Heterogeneous and homogeneous objects are allowed.
Constructors of ArrayLsit :
1. ArrayList
a=new ArrayList();
Creates an empty
ArrayList object with default initial capacity "10".
if
ArrayList reaches its max capacity then a new ArrayList object will be created
with
New capacity =
(current capacity*3/2)+1
2. ArrayList
a=new ArrayList(int initialcapacity);
Creates an empty
ArrayList object with the specified initial capacity.
3. ArrayList
a=new ArrayList(collection c);
Creates an
equivalent ArrayList object for the given Collection that is this constructor
meant for inter conversation between collection objects. That is to dance
between collection objects.
Implementation.
1)A List represents a data structure, which allows to
dynamically add,access and remove objects of the same and different type.
2)Adding objects to the list is usually done via the add()
method.
3) The get(int i)
method allows to retrieve the element at position i.
4) remove(int i) method allows to remove the element at
position.
Example :
package com.coding.core;
import java.util.Arrays;
public class CustomArrayList<E> {
private int size = 0;
// Default Size 10
private static final int DEFAULT_CAPACITY = 10;
// Object Array declaration
private Object elements[];
// Initialize the Object array with default capacity
public CustomArrayList() {
elements = new Object[DEFAULT_CAPACITY];
}
/*
* 1) Before add the elements to an Object Array we should check the size.
* 2) If the size and length of array equal the needs to be increase the
* size by calling ensureCapacity()
*/
public void add(E e) {
if (size == elements.length) {
ensureCapa();
}
elements[size++] = e;
}
public int size() {
return size;
}
// Increase the capacity of object array and copy all the elements to new
// Array.
private void ensureCapa() {
int newSize = elements.length * 2;
elements = Arrays.copyOf(elements, newSize);
}
@SuppressWarnings("unchecked")
public E get(int i) {
if (i >= size || i < 0) {
throw new IndexOutOfBoundsException("Index: " + i + ", Size " + i);
}
return (E) elements[i];
}
// Remove the specified position element from
public Object remove(int i) {
if (i < size) {
Object obj = elements[i];
elements[i] = null;
int tmp = i;
while (tmp < size) {
elements[tmp] = elements[tmp + 1];
elements[tmp + 1] = null;
tmp++;
}
size--;
return obj;
} else {
throw new ArrayIndexOutOfBoundsException();
}
}
}
Test case:
package com.coding.core;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class MyListTest {
@Test(expected = IndexOutOfBoundsException.class)
public void testMyList() {
CustomArrayList<Integer> list = new CustomArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(3);
list.add(4);
assertTrue(4 == list.get(4));
assertTrue(2 == list.get(1));
assertTrue(3 == list.get(2));
list.get(6);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testNegative() {
CustomArrayList<Integer> list = new CustomArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(3);
list.add(4);
list.get(-1);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testList() {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(3);
list.add(4);
assertTrue(4 == list.get(4));
assertTrue(2 == list.get(1));
assertTrue(3 == list.get(2));
list.get(6);
}
}
Example :
package com.coding.core;
import java.util.Arrays;
public class CustomArrayList<E> {
private int size = 0;
// Default Size 10
private static final int DEFAULT_CAPACITY = 10;
// Object Array declaration
private Object elements[];
// Initialize the Object array with default capacity
public CustomArrayList() {
elements = new Object[DEFAULT_CAPACITY];
}
/*
* 1) Before add the elements to an Object Array we should check the size.
* 2) If the size and length of array equal the needs to be increase the
* size by calling ensureCapacity()
*/
public void add(E e) {
if (size == elements.length) {
ensureCapa();
}
elements[size++] = e;
}
public int size() {
return size;
}
// Increase the capacity of object array and copy all the elements to new
// Array.
private void ensureCapa() {
int newSize = elements.length * 2;
elements = Arrays.copyOf(elements, newSize);
}
@SuppressWarnings("unchecked")
public E get(int i) {
if (i >= size || i < 0) {
throw new IndexOutOfBoundsException("Index: " + i + ", Size " + i);
}
return (E) elements[i];
}
// Remove the specified position element from
public Object remove(int i) {
if (i < size) {
Object obj = elements[i];
elements[i] = null;
int tmp = i;
while (tmp < size) {
elements[tmp] = elements[tmp + 1];
elements[tmp + 1] = null;
tmp++;
}
size--;
return obj;
} else {
throw new ArrayIndexOutOfBoundsException();
}
}
public static void main(String[] args) {
CustomArrayList<Integer> mal = new CustomArrayList<Integer>();
mal.add(new Integer(2));
mal.add(new Integer(5));
mal.add(new Integer(1));
mal.add(new Integer(23));
mal.add(new Integer(14));
System.out.println("************************************");
for (int i = 0; i < mal.size(); i++) {
System.out.println("array list elements are : " + mal.get(i) + " ");
}
System.out.println("**********************************");
mal.add(new Integer(29));
System.out.println("Element at Index 5:" + mal.get(5));
System.out.println("*********************************");
System.out.println();
System.out.println("List size: " + mal.size());
System.out.println("********************************");
System.out.println("removed elem's at index 2: " + mal.remove(2));
System.out.println();
for (int i = 0; i < mal.size(); i++) {
System.out.println("after Removing elems are :" + mal.get(i) + " ");
}
}
}
Test case:
package com.coding.core;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class MyListTest {
@Test(expected = IndexOutOfBoundsException.class)
public void testMyList() {
CustomArrayList<Integer> list = new CustomArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(3);
list.add(4);
assertTrue(4 == list.get(4));
assertTrue(2 == list.get(1));
assertTrue(3 == list.get(2));
list.get(6);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testNegative() {
CustomArrayList<Integer> list = new CustomArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(3);
list.add(4);
list.get(-1);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testList() {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(3);
list.add(4);
assertTrue(4 == list.get(4));
assertTrue(2 == list.get(1));
assertTrue(3 == list.get(2));
list.get(6);
}
}
No comments:
Post a Comment