MyArrayList Implementation
In this blog, the assignment was to show implementation of the set and get method for ArrayList. I tried to make but I arrived at this following code in which MyArrayList extends directly AbstractList. In MyArrayList, I used get and set method. This is not a dynamic actual custom implementation of ArrayList.CODE:
import java.util.*;
public class TestArrayList{
private static class MyArrayList extends AbstractList
{
private Object a[];
MyArrayList(Object array[]) {
a = array;
}
public Object get(int index) {
return a[index];
}
public Object set(int index, Object element) {
Object oldValue = a[index];
a[index] = element;
return oldValue;
}
public int size() {
return a.length;
}
}
public static void main(String args[])
{
Object[] bArray= new Object[4];
bArray[0] =new Integer(12);
bArray[1] =new Integer(10);
bArray[2] =new Integer(14);
bArray[3] =new Integer(16);
MyArrayList aArray = new MyArrayList(bArray);
System.out.println("Previous to change \t" + aArray.get(1));
aArray.set(1,new Integer(40));
System.out.println("After changing value \t" +aArray.get(1));
System.out.println("Size of the array \t" +aArray.size());
}
}
OUTPUT:
Previous to change 10
After changing value 40
Size of the array 4
0 Comments:
Post a Comment
<< Home