package Inter;
import java.util.Arrays;
import java.util.Iterator;
/**
*
* @author chinmaykumar.t
*/
public class CustomArrayList {
private int size = 0;
Object[] objArr = null;
public CustomArrayList() {
objArr = new Object[10];
}
public Object get(int index) {
if (index < size) {
return objArr[index];
} else {
throw new ArrayIndexOutOfBoundsException();
}
}
public void add(Object obj) {
if (objArr.length - size <= 5) {
increaseListSize();
}
objArr[size++] = obj;
}
public Object remove(int index) {
if (index < size) {
Object object = objArr[index];
objArr[index] = null;
int temp = index;
while (temp < size) {
objArr[temp] = objArr[temp + 1];
temp++;
}
size--;
return object;
} else {
throw new ArrayIndexOutOfBoundsException();
}
}
public void increaseListSize() {
objArr = Arrays.copyOf(objArr, objArr.length * 2);
System.out.println("\nNew length: " + objArr.length);
}
public int size() {
return size;
}
public static void main(String[] args) {
CustomArrayList arrayList=new CustomArrayList();
arrayList.add("chinmay");
arrayList.add("kumar");
arrayList.add("tripathy");
System.out.println(""+arrayList.toString());
System.out.println(""+arrayList.get(0));
System.out.println(""+arrayList.remove(1));
System.out.println(""+arrayList.size());
for(int i=0;i<arrayList.size();i++){
System.out.print(arrayList.get(i)+" ");
}
}
}
import java.util.Arrays;
import java.util.Iterator;
/**
*
* @author chinmaykumar.t
*/
public class CustomArrayList {
private int size = 0;
Object[] objArr = null;
public CustomArrayList() {
objArr = new Object[10];
}
public Object get(int index) {
if (index < size) {
return objArr[index];
} else {
throw new ArrayIndexOutOfBoundsException();
}
}
public void add(Object obj) {
if (objArr.length - size <= 5) {
increaseListSize();
}
objArr[size++] = obj;
}
public Object remove(int index) {
if (index < size) {
Object object = objArr[index];
objArr[index] = null;
int temp = index;
while (temp < size) {
objArr[temp] = objArr[temp + 1];
temp++;
}
size--;
return object;
} else {
throw new ArrayIndexOutOfBoundsException();
}
}
public void increaseListSize() {
objArr = Arrays.copyOf(objArr, objArr.length * 2);
System.out.println("\nNew length: " + objArr.length);
}
public int size() {
return size;
}
public static void main(String[] args) {
CustomArrayList arrayList=new CustomArrayList();
arrayList.add("chinmay");
arrayList.add("kumar");
arrayList.add("tripathy");
System.out.println(""+arrayList.toString());
System.out.println(""+arrayList.get(0));
System.out.println(""+arrayList.remove(1));
System.out.println(""+arrayList.size());
for(int i=0;i<arrayList.size();i++){
System.out.print(arrayList.get(i)+" ");
}
}
}