package me.wwsun.list;

import java.util.Iterator;

public class MyArrayList<T> implements Iterable<T> {

    private static final int DEFAULT_CAPACITY = 10;

    private int theSize;
    private T[] theItems;

    public MyArrayList() {
        clear();
    }

    public void clear() {
        theSize = 0;
        ensureCapacity(DEFAULT_CAPACITY);
    }

    public void ensureCapacity(int newCapacity) {
        if (newCapacity < theSize) return;

        T[] old = theItems;
        theItems = (T[]) new Object[newCapacity];
        for (int i = 0; i < size(); i++) {
            theItems[i] = old[i];
        }
    }

    public int size() {
        return theSize;
    }

    public boolean isEmpty() {
        return size() == 0;
    }

    public void trimToSize() {
        ensureCapacity(size());
    }

    public T get(int index) {
        if (index < 0 || index >= size()) throw new ArrayIndexOutOfBoundsException();

        return theItems[index];
    }

    public T set(int index, T newVal) {
        if (index < 0 || index >= size()) throw new ArrayIndexOutOfBoundsException();
        T old = theItems[index];
        theItems[index] = newVal;
        return old;
    }

    /**
     * add the element to the end of list
     *
     * @param x is the element you want to add
     * @return true if add successfully, otherwise return false
     */
    public boolean add(T x) {
        add(size(), x);
        return true;
    }

    /**
     * add the element to the specific position
     *
     * @param index is the position you want to insert the element
     * @param x     is the element you want to insert
     */
    public void add(int index, T x) {
        if (theItems.length == size()) ensureCapacity(size() * 2 + 1);
        for (int i = theSize; i > index; i--) {
            theItems[i] = theItems[i - 1];
        }
        theItems[index] = x;
        theSize++;
    }

    public T remove(int index) {
        T removeItem = theItems[index];
        for (int i = index; i < size() - 1; i++) {
            theItems[i] = theItems[i + 1];
        }
        theSize--;
        return removeItem;
    }


    public Iterator<T> iterator() {
        return new ArrayListIterator();
    }

    // inner class
    private class ArrayListIterator implements java.util.Iterator<T> {

        private int current = 0;

        public boolean hasNext() {
            return current < size();
        }

        public T next() {
            if (!hasNext()) throw new java.util.NoSuchElementException();
            return theItems[current++];
        }

        public void remove() {
            MyArrayList.this.remove(--current); // reference the outer class
        }
    }
}

@@ -45,16 +45,78 @@
 rator;%0A%0A
+/**%0A *%0A * @param %3CE%3E is the type of elements in this list%0A */%0A
 public c
@@ -132,17 +132,17 @@
 rayList%3C
-T
+E
 %3E implem
@@ -155,17 +155,17 @@
 terable%3C
-T
+E
 %3E %7B%0A%0A   
@@ -251,17 +251,17 @@
 private 
-T
+E
 %5B%5D theIt
@@ -515,17 +515,17 @@
         
-T
+E
 %5B%5D old =
@@ -555,17 +555,17 @@
 tems = (
-T
+E
 %5B%5D) new 
@@ -881,17 +881,17 @@
  public 
-T
+E
  get(int
@@ -1037,17 +1037,17 @@
  public 
-T
+E
  set(int
@@ -1054,17 +1054,17 @@
  index, 
-T
+E
  newVal)
@@ -1160,17 +1160,17 @@
         
-T
+E
  old = t
@@ -1317,17 +1317,23 @@
  @param 
-x
+element
  is the 
@@ -1451,19 +1451,25 @@
 ean add(
-T x
+E element
 ) %7B%0A    
@@ -1484,17 +1484,23 @@
 size(), 
-x
+element
 );%0A     
@@ -1666,17 +1666,23 @@
  @param 
-x
+element
      is 
@@ -1755,11 +1755,17 @@
 ex, 
-T x
+E element
 ) %7B%0A
@@ -1962,17 +1962,23 @@
 ndex%5D = 
-x
+element
 ;%0A      
@@ -2008,17 +2008,17 @@
  public 
-T
+E
  remove(
@@ -2038,17 +2038,17 @@
         
-T
+E
  removeI
@@ -2248,17 +2248,17 @@
 terator%3C
-T
+E
 %3E iterat
@@ -2396,17 +2396,17 @@
 terator%3C
-T
+E
 %3E %7B%0A%0A   
@@ -2534,17 +2534,17 @@
  public 
-T
+E
  next() 
@@ -2795,9 +2795,8 @@
 %0A    %7D%0A%7D
-%0A

update MyArrayList