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; /** *

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."); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!