Name: Nguyen Huu Thang ID Student: GCD201443 Class: GCD1001 Lap 1
Name: Nguyen Huu Thang ID Student: GCD201443 Class: GCD1001 Lap 1
Name: Nguyen Huu Thang ID Student: GCD201443 Class: GCD1001 Lap 1
ID Student: GCD201443
Class: GCD1001
LAP 1
package implementations;
import interfaces.List;
import java.util.Arrays;
import java.util.Iterator;
public ArrayList(){
elements = new Object[DEFAULT_CAPACITY];
}
@Override
public boolean add(E element) {
if (elements.length == size){
elements = grow();
}
elements[size++] = element;
return true;
}
private Object[] grow(){
return Arrays.copyOf(elements, elements.length*2);
}
@Override
public boolean add(int index, E element) {
checkIndex(index);
if (elements.length ==size){
elements = grow();
}
for (int i = size;i>=index +1;i--){
elements[i] =elements[i-1];
}
elements[index]= element;
size++;
return true;
}
private void checkIndex(int index){
if (index<0 ||index>=size) throw new IndexOutOfBoundsException("Index
out of bound:"
+index +" out of "+size);
}
private E getElement(int index){
return (E)elements[index];
}
@Override
public E get(int index) {
checkIndex(index);
return getElement(index);
}
@Override
public E set(int index, E element) {
checkIndex(index);
elements[index] = element;
return null;
}
private Object[] shrink(){
return Arrays.copyOf(elements,elements.length/2);
}
private void ensureCapacity(){
if (size < elements.length/3) {
elements = shrink();
}
}
public int getCapacity(){
return elements.length;
}
@Override
public E remove(int index) {
checkIndex(index);
E element = getElement(index);
for (int i=index;i<size-1;i++){
elements[i] = elements[i+1];
}
size--;
ensureCapacity();
return element;
}
@Override
public int size() {
return size;
}
public String toString(){
StringBuilder result = new StringBuilder();
// result.append();
for (int i = 0;i<size;i++){
if (i==size-1) result.append(elements[i]);
else result.append(elements[i]+"\n");
}
return result.toString();
}
@Override
public int indexOf(E element) {
for (int i=0;i<size;i++){
if (elements[i].equals(element)){
return i;
}
}
return -1;
}
@Override
public boolean contains(E element) {
return indexOf(element) !=-1;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
private int index = 0;
@Override
public boolean hasNext() {
return index<size;
}
@Override
public E next() {
E element = getElement(index);
index++;
return element;
}
};
}
}
package implementations;
import interfaces.AbstractStack;
import java.util.Iterator;
@Override
public void push(E element) {
Node<E> newNode = new Node<>(element);
newNode.previous = top;
top = newNode;
size++;
}
@Override
public E pop() {
ensureNonEmpty();
E data = top.element;
top = top.previous;
size--;
return data;
}
@Override
public String toString() {
Node<E> current = top;
StringBuilder result = new StringBuilder();
while(current!=null){
result.append(current.element+ " ");
current = current.previous;
}
return result.toString();
}
@Override
public E peek() {
return null;
}
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
Node<E> current = top;
@Override
public boolean hasNext() {
return current!= null;
}
@Override
public E next() {
E element = current.element;
current = current.previous;
return element;
}
};
}
}
import implementations.*;
import java.util.Iterator;
import java.util.Scanner;
}
public static void testArrayList(){
ArrayList<Integer> myArrayList = new ArrayList<>();
myArrayList.add(3);
myArrayList.add(3);
myArrayList.add(4);
myArrayList.add(5);
myArrayList.add(3);
System.out.println(myArrayList);
myArrayList.add(2,8);
myArrayList.set(4,10);
System.out.println("After adding:");
System.out.println(myArrayList);
System.out.println("Index of 5 is "+ myArrayList.indexOf(10));
System.out.println("Delete element "+ myArrayList.remove(1) + " out
of Arraylist");
System.out.println("Delete element "+ myArrayList.remove(1) + " out
of Arraylist");
System.out.println("Delete element "+ myArrayList.remove(1) + " out
of Arraylist");
System.out.println("Delete element "+ myArrayList.remove(1) + " out
of Arraylist");
System.out.println("Delete element "+ myArrayList.remove(1) + " out
of Arraylist");
System.out.println(myArrayList);
System.out.println("Capacity of the Arraylist "+
myArrayList.getCapacity());