package ru.matevosyan;

/**
 * DynamicArray class.
 * Created on 18.05.2017.
 * @author Matevosyan Vardan
 * @version 1.0
 */

public class DynamicArray<E> implements IDynamicArray<E> {

    private Object[] container;
    private static final Object[] EMPTYARRAY = {};
    private static final int DEFAULTARRAYSIZE = 10;
    private int index = 0;

    public DynamicArray(int size) {
        if (size > 0) {
            this.container = new Object[size];
        } else if (size == 0) {
            this.container = EMPTYARRAY;
        } else {
            throw new IllegalArgumentException("Please pass correct array size");
        }
    }

    public DynamicArray() {
        this.container = new Object[DEFAULTARRAYSIZE];
    }

    @Override
    public void add(E value) {
        checkSize(this.index + 1);
        this.container[this.index++] = value;

    }



    @Override
    @SuppressWarnings("unchecked")
    public E get(int index) {
        return (E) this.container[index];
    }

    private void checkSize(int nextIndex) {
        if (this.container.length - 1 == nextIndex) {
            int nexSize = (this.container.length * 3) / 2 + 1;
            Object[] container = new Object[nexSize];
            System.arraycopy(this.container, 0, container, 0, this.container.length);

        }
    }
}
@@ -17,16 +17,69 @@
 osyan;%0A%0A
+import java.util.Arrays;%0Aimport java.util.Iterator;%0A%0A
 /**%0A * D
@@ -229,17 +229,30 @@
 Array%3CE%3E
- 
+,  Iterable%3CE%3E
 %7B%0A%0A    p
@@ -409,16 +409,164 @@
 x = 0;%0A%0A
+    /**%0A     * Constructor for creating default size for dynamic array or if size is 0 create an empty array.%0A     * @param size of array.%0A     */%0A%0A
     publ
@@ -861,356 +861,1344 @@
     
-public DynamicArray() %7B%0A        this.container = new Object%5BDEFAULTARRAYSIZE%5D;%0A    %7D%0A%0A    @Override%0A    public void add(E value) %7B%0A        checkSize(this.index + 1);%0A        this.container%5Bthis.index++%5D = value;%0A%0A    %7D%0A%0A%0A%0A    @Override%0A    @SuppressWarnings(%22unchecked%22)%0A    public E get(int index) %7B%0A        return (E) this.container%5Bindex%5D;%0A    %7D
+/**%0A     * If arrays size was not passed to constructor, create an array with default size.%0A     */%0A%0A    public DynamicArray() %7B%0A        this.container = new Object%5BDEFAULTARRAYSIZE%5D;%0A    %7D%0A%0A    /**%0A     * Create to add value to an array.%0A     * @param value is value that type which you declare in generics.%0A     * @throws ArrayIndexOutOfBoundsException if index is bigger than actual.%0A     */%0A%0A    @Override%0A    public void add(E value) throws ArrayIndexOutOfBoundsException %7B%0A        checkSize(this.index + 1);%0A        this.container%5Bthis.index++%5D = value;%0A%0A    %7D%0A%0A    /**%0A     * Create to get the value from an array.%0A     * @param index position value in an array.%0A     * @return value that type that was declare in generic diamonds.%0A     * @throws IllegalArgumentException if is not correct index size.%0A     * @throws ArrayIndexOutOfBoundsException if index is bigger than actual.%0A     */%0A%0A    @Override%0A    @SuppressWarnings(%22unchecked%22)%0A    public E get(int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException %7B%0A        return (E) this.container%5Bindex%5D;%0A    %7D%0A%0A    /**%0A     * Check array size and rise it when an array length is equal or bigger that default size.%0A     * Or if it is the last index that going to be hold by passing value.%0A     * @param nextIndex is next index that going to be checked.%0A     */
 %0A%0A  
@@ -2281,32 +2281,77 @@
 - 1 == nextIndex
+ && this.container.length %3E= DEFAULTARRAYSIZE
 ) %7B%0A            
@@ -2413,25 +2413,21 @@
         
-Object%5B%5D 
+this.
 containe
@@ -2434,27 +2434,46 @@
 r = 
-new Object%5B
+Arrays.copyOf(this.container, 
 nexSize
-%5D
+)
 ;%0A  
@@ -2482,96 +2482,570 @@
     
+%7D%0A
     
-System.arraycopy(this.
+%7D%0A%0A    /**%0A     * Override iterator method from interface Iterable.%0A     * @return an instance of Iterator type.%0A     */%0A%0A    @Override%0A    public Iterator%3CE%3E iterator() %7B%0A%0A        return new Iterator%3CE%3E() %7B%0A%0A            int 
 co
+u
 nt
-ainer, 0, container, 0, this.container.length);%0A%0A        %7D
+ = 0;%0A            @Override%0A            public boolean hasNext() %7B%0A                return count %3C container.length - (container.length - index);%0A            %7D%0A%0A            @Override%0A            @SuppressWarnings(%22unchecked%22)%0A            public E next() %7B%0A                return (E) container%5Bcount++%5D;%0A            %7D%0A        %7D;
 %0A   

add javadoc and override iterator() method from Iterable interface