package ru.job4j;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * Task 5.3.1
 * Create model for PseodoArrayList
 * @author  Anton Vasilyuk on 12.06.2017.
 * @version 1.0
 */
public class PseodoArrayList<E> implements SimpleContainers<E> {

    /**.
     * @size is size array
     */
    private int size;

    /**.
     * @cursor is a place cursor
     */
    private int cursor = 0;

    /**.
     * @container is array objects
     */
    private Object[] container;

    /**.
     * @numElement is amount objects
     */
    private int numElement = 0;

    /**.
     * Constructor fot this class
     * @param size
     */
    public PseodoArrayList(int size) {
        this.container = new Object[size];
        this.size = size;
    }

    /**.
     * Method for add object to array
     * @param value
     */
    public void add(E value) {
        if (this.sizeObjects() < size) {
            this.container[cursor++] = (Object) value;
        } else {
            int sizeTemp = size * 2;
            Object[] newContainer = new Object[sizeTemp];
            System.arraycopy(container, 0, newContainer, 0, size);
            container = newContainer;
            size = sizeTemp;
            this.container[cursor++] = (Object) value;
        }
    }

    /**.
     * Method for return object from index position
     * @param index
     * @return
     */
    public E get(int index) {
        cursor = 0;
        for (int i = 0; i < index; i++) {
            if (iterator().hasNext()) {
                iterator().next();
                cursor++;
            } else {
                throw new NoSuchElementException("There is not such index");
            }
        }
        return (E) container[cursor];
    }

    /**.
     * Realisation iterable
     * @return
     */
    @Override
    public Iterator<E> iterator() {
        Iterator<E> it = (Iterator<E>) new Iterator<Object>() {

            private int cursorIter = 0;

            @Override
            public boolean hasNext() {
                return cursor < size;
            }

            @Override
            public Object next() {
                if (hasNext()) {return container[cursor];}
                else {throw new NoSuchElementException("Need add cell in this array");}
            }

            @Override
            public void remove() {
                container[cursor] = null;
                cursor--;
            }
        };
        return it;
    }

    /**.
     * Method for checking size container
     * @return
     */
    public int sizeObjects() {
        numElement = 0;
        for (int i = 0; i < container.length; i++) {
            if (container[i] != null) {numElement++;}
        }
        return numElement;
    }
}

@@ -408,33 +408,32 @@
 or = 0;%0A%0A    /**
-.
 %0A     * @contain
@@ -430,112 +430,119 @@
 * @c
-ontainer is array objects%0A     */%0A    private Object%5B%5D container;%0A%0A    /**.%0A     * @numElement is amount
+ursorIter is position for iterator%0A      */%0A    private int cursorIter = 0;%0A    /**.%0A     * @container is array
  obj
@@ -570,26 +570,26 @@
 ate 
-int numElement = 0
+Object%5B%5D container
 ;%0A%0A 
@@ -894,26 +894,14 @@
 if (
-this.sizeObjects()
+cursor
  %3C s
@@ -940,36 +940,47 @@
 r%5Bcursor
-++
 %5D = 
-(Object) value
+value;%0A            cursor++
 ;%0A      
@@ -1256,28 +1256,39 @@
 rsor
-++
 %5D = 
-(Object) value
+value;%0A            cursor++
 ;%0A  
@@ -1444,26 +1444,31 @@
         
-cursor = 0
+E result = null
 ;%0A      
@@ -1473,301 +1473,165 @@
     
+i
 f
-or
  (in
-t i = 0; i %3C index; i++) %7B%0A            if (iterator().hasNext()) %7B%0A                iterator().next();%0A                cursor++;%0A            %7D else %7B%0A                throw new NoSuchElementException(%22There is not such index%22);%0A            %7D%0A        %7D%0A        return (E) container%5Bcursor%5D
+dex %3C cursor) %7Bresult = (E) container%5Bindex%5D;%7D%0A        else %7Bthrow new IndexOutOfBoundsException(%22On this position no element%22);%7D%0A        return result
 ;%0A  
@@ -1814,49 +1814,8 @@
  %7B%0A%0A
-            private int cursorIter = 0;%0A%0A
     
@@ -1896,24 +1896,28 @@
 eturn cursor
+Iter
  %3C size;%0A   
@@ -2040,16 +2040,22 @@
 r%5Bcursor
+Iter++
 %5D;%7D%0A    
@@ -2238,24 +2238,28 @@
 ainer%5Bcursor
+Iter
 %5D = null;%0A  
@@ -2278,16 +2278,20 @@
   cursor
+Iter
 --;%0A    
@@ -2450,16 +2450,20 @@
         
+int 
 numEleme

add file for chapter_003_5.3.1