Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The ArrayList class given below should implement Iterator interface Define the m

ID: 3805029 • Letter: T

Question

The ArrayList class given below should implement Iterator interface

Define the methdods from the Iterator interface

import java.util.*;
public class ArrayList{

Object[] data;

/**
* This is the default "no-arg" constructor.
* @param Nothing.
* @return Nothing.
*/
public ArrayList() {
data = new Object[0];
}

/**
* This method inserts an Object into a specified index.
* @param obj This is the object to be stored in ArrayList.
* @param index This is the index for which the object is to be stored
* in the ArrayList.
* @return Nothing.
* @exception IndexOutOfBoundsException On negative index or index
* that is greater than the ArrayList's size error.
*/
public void insert(Object obj, int index) throws
IndexOutOfBoundsException {
if (index >= 0 && index <= data.length) {
Object[] temp = new Object[data.length + 1];
for (int i = 0; i < index; i++) {
temp[i] = data[i];
}
temp[index] = obj;
for (int i = index + 1; i < temp.length; i++) {
temp [i] = data[i - 1];
}
data = temp;
} else {
throw new IndexOutOfBoundsException
("Error: index out of bounds.");
}
}

/**
* This method removes an Object from a specified index.
* @param index This is the index for which the object is to be removed
* from the ArrayList.
* @return Object This returns the Object that is being removed.
* @exception IndexOutOfBoundsException On negative index or index
* that is greater than or equal to the ArrayList's size error.
*/
public Object remove(int index) throws IndexOutOfBoundsException {
if (index >= 0 && index < data.length) {
Object[] temp = new Object[data.length - 1];
for (int i = 0; i < index; i++) {
temp[i] = data[i];
}
for (int i = index; i < temp.length; i++) {
temp [i] = data[i + 1];
}
Object tempObj = data[index];
data = temp;
return tempObj;
} else {
throw new IndexOutOfBoundsException
("Error: index out of bounds.");
}
}

/**
* This method returns the size of the ArrayList.
* @param Nothing.
* @return int This returns the size of the ArrayList.
*/
public int size() {
return data.length;
}

/**
* This method returns all elements in the ArrayList in an organized String.
* @param Nothing.
* @return String This returns all elements in the ArrayList as a String.
*/
public String toString() {
String str = "{";
if (data.length != 0) {
for (int i = 0; i < data.length - 1; i++) {
str += data[i] + ", ";
}
str += data[data.length - 1];
}
str += "}";
return str;
}

/**
* This method checks if the ArrayList is empty.
* @param Nothing.
* @return boolean This returns true if the ArrayList is empty, returns
* false otherwise.
*/
public boolean isEmpty() {
return size() == 0;
}

/**
* This method finds the index of a specified Object.
* @param target This is the Object that is being asked to find
* the index of.
* @return int This returns the index of the specified Object in
* the ArrayList, returns -1 if Object is not found in the ArrayList.
*/
public int indexOf(Object target) {
for (int i = 0; i < data.length; i++) {
if (data[i].equals(target)) {
return i;
}
}
return -1;
}

/**
* This method checks if this ArrayList is equal to another ArrayList.
* @param other This is the other ArrayList to compare.
* @return boolean This returns true if all elements in this ArrayList
* equals to all elements in the other ArrayList, returns false otherwise.
*/
public boolean equals(Object other) {
ArrayList otherAL = (ArrayList) other;
if (data.length == otherAL.size()) {
for (int i = 0; i < data.length; i++) {
if (!data[i].equals(otherAL.get(i))) {
return false;
}
}
return true;
} else {
return false;
}
}

/**
* This method returns the element in ArrayList using the specified index.
* @param index This specifies which index's Object the method should get.
* @return Object This returns the Object of the specified index.
* @exception IndexOutOfBoundsException On negative index or index
* that is greater than or equal to the ArrayList's size error.
*/
public Object get(int index) throws IndexOutOfBoundsException {
if (index >= 0 && index < data.length) {
return data[index];
} else {
throw new IndexOutOfBoundsException
("Error: index out of bounds.");
}
}
}

Explanation / Answer

import java.util.*;
class ArrayList implements Iterable<Object>{

    Object[] data;

    /**
     * This is the default "no-arg" constructor.
     * @param Nothing.
     * @return Nothing.
     */
    public ArrayList() {
        data = new Object[0];
    }

    /**
     * This method inserts an Object into a specified index.
     * @param obj This is the object to be stored in ArrayList.
     * @param index This is the index for which the object is to be stored
     * in the ArrayList.
     * @return Nothing.
     * @exception IndexOutOfBoundsException On negative index or index
     * that is greater than the ArrayList's size error.
     */
    public void insert(Object obj, int index) throws
    IndexOutOfBoundsException {
        if (index >= 0 && index <= data.length) {
            Object[] temp = new Object[data.length + 1];
            for (int i = 0; i < index; i++) {
                temp[i] = data[i];
            }
            temp[index] = obj;
            for (int i = index + 1; i < temp.length; i++) {
                temp [i] = data[i - 1];
            }
            data = temp;
        } else {
            throw new IndexOutOfBoundsException
            ("Error: index out of bounds.");
        }
    }

    /**
     * This method removes an Object from a specified index.
     * @param index This is the index for which the object is to be removed
     * from the ArrayList.
     * @return Object This returns the Object that is being removed.
     * @exception IndexOutOfBoundsException On negative index or index
     * that is greater than or equal to the ArrayList's size error.
     */
    public Object remove(int index) throws IndexOutOfBoundsException {
        if (index >= 0 && index < data.length) {
            Object[] temp = new Object[data.length - 1];
            for (int i = 0; i < index; i++) {
                temp[i] = data[i];
            }      
            for (int i = index; i < temp.length; i++) {
                temp [i] = data[i + 1];
            }          
            Object tempObj = data[index];
            data = temp;
            return tempObj;
        } else {
            throw new IndexOutOfBoundsException
            ("Error: index out of bounds.");
        }
    }

    /**
     * This method returns the size of the ArrayList.
     * @param Nothing.
     * @return int This returns the size of the ArrayList.
     */
    public int size() {
        return data.length;
    }

    /**
     * This method returns all elements in the ArrayList in an organized String.
     * @param Nothing.
     * @return String This returns all elements in the ArrayList as a String.
     */
    public String toString() {
        String str = "{";
        if (data.length != 0) {
            for (int i = 0; i < data.length - 1; i++) {
                str += data[i] + ", ";
            }
            str += data[data.length - 1];
        }
        str += "}";
        return str;
    }

    /**
     * This method checks if the ArrayList is empty.
     * @param Nothing.
     * @return boolean This returns true if the ArrayList is empty, returns
     * false otherwise.
     */
    public boolean isEmpty() {
        return size() == 0;
    }

    /**
     * This method finds the index of a specified Object.
     * @param target This is the Object that is being asked to find
     * the index of.
     * @return int This returns the index of the specified Object in
     * the ArrayList, returns -1 if Object is not found in the ArrayList.
     */
    public int indexOf(Object target) {
        for (int i = 0; i < data.length; i++) {
            if (data[i].equals(target)) {
                return i;
            }
        }
        return -1;
    }

    /**
     * This method checks if this ArrayList is equal to another ArrayList.
     * @param other This is the other ArrayList to compare.
     * @return boolean This returns true if all elements in this ArrayList
     * equals to all elements in the other ArrayList, returns false otherwise.
     */
    public boolean equals(Object other) {
        ArrayList otherAL = (ArrayList) other;
        if (data.length == otherAL.size()) {
            for (int i = 0; i < data.length; i++) {

                if (!data[i].equals(otherAL.get(i))) {
                    return false;
                }
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * This method returns the element in ArrayList using the specified index.
     * @param index This specifies which index's Object the method should get.
     * @return Object This returns the Object of the specified index.
     * @exception IndexOutOfBoundsException On negative index or index
     * that is greater than or equal to the ArrayList's size error.
     */
    public Object get(int index) throws IndexOutOfBoundsException {
        if (index >= 0 && index < data.length) {
            return data[index];
        } else {
            throw new IndexOutOfBoundsException
            ("Error: index out of bounds.");
        }
    }

   @Override
   public Iterator<Object> iterator() {
       return new ArrayIterator<>();
   }
   public class ArrayIterator<Object> implements Iterator<Object>{
       int currentIndex = 0;
       @Override
       public boolean hasNext() {
           if(currentIndex>=data.length)
               return false;
           else
               return true;
       }

       @Override
       public Object next() {
           Object obj = (Object)get(currentIndex);
           currentIndex++;
           return obj;
       }

       @Override
       public void remove() throws IndexOutOfBoundsException{
           ArrayList.this.remove(currentIndex);
       }
      
   }
}