public class Array<E> {
int size;
E[] data;
public Array(int capacity) {
data=(E[])new Object[capacity];
size=0;
}
public Array() {
data=(E[])new Object[10];
size=0;
}
public int getSize() {
return size;
}
public int getCapacity() {
return data.length;
}
public boolean isEmpty() {
return size==0;
}
public void add(int index,E e) {
if (size==getCapacity())
resize(2*data.length);
if (index<0||index>size)
throw new IllegalArgumentException("index ");
for (int i = data.length-1; i >=index; i--) {
data[i+1]=data[i];
}
data[size]=e;
size++;
}
public void addLast(E e) {
add(size, e);
}
public void addFirst(E e) {
add(0, e);
}
public E get(int index) {
if (index<0||index>size)
throw new IllegalArgumentException("index ");
return data[index];
}
public E getLast() {
return get(data.length);
}
public void set(int index,E e) {
if (index<0||index>size)
throw new IllegalArgumentException("index ");
data[index]=e;
}
public boolean contains(E e) {
for (int i = 0; i < size; i++) {
if (data[i]==e) {
return true;
}
}
return false;
}
public int find(E e) {
for (int i = 0; i < size; i++) {
if (data[i]==e) {
return i;
}
}
return -1;
}
public E remove(int index) {
if (index<0||index>=size)
throw new IllegalArgumentException("index ");
E res=data[index];
for (int i = index+1; i < data.length; i++) {
data[i-1]=data[i];
}
size--;
data[size]=null;
if (size==data.length/4) {
resize(data.length/2);
}
return res;
}
public E removeFirst() {
return remove(0);
}
public E removeLast() {
return remove(data.length);
}
public boolean removeElement(E e) {
int res = find(e);
if (res!=-1) {
remove(res);
return true;
}
return false;
}
private void resize(int newCapacity) {
E[] newdata=(E[])new Object[newCapacity];
for (int i = 0; i < data.length; i++) {
newdata[i]=data[i];
}
data=newdata;
}
}
public class Queue<E> {
Array<E> array;
public Queue() {
array=new Array<E>();
}
public Queue(int capacity) {
array=new Array<E>(capacity);
}
void enqueue(E e) {
array.addLast(e);
}
void dequeue(E e) {
array.removeFirst();
}
void getFront(E e) {
array.get(0);
}
int getSize() {
return array.getSize();
}
boolean isEmpty(){
return array.isEmpty();
}
}