Question: Array with Iterator. Java style CANNOT USE SET OR ADD AT AN INDEX TO INSERT AN ELEMENT BECAUSE THAT WILL THROW AN EXCEPTION arrayIndexOutOfBounds. DO

Array with Iterator. Java style

CANNOT USE SET OR ADD AT AN INDEX TO INSERT AN ELEMENT BECAUSE THAT WILL THROW AN EXCEPTION arrayIndexOutOfBounds. DO NOT ANSWER IF YOU DONT KNOW BECAUSE ANY SPAM ANSWERS WILL BE REPORTED.

Implement an array data structure as a class JstyArray to support the Iterable interface such that the following code works:

JstyArray data; data = new JstyArray(10); for (int i = 0; i < 10; ++i) { data.set(i, new Integer(i) ); } int sum = 0; for ( int v : data ) { if (v == null) continue; // empty cell sum += v; } 

The iterator provided by this class follows the behaviour of typical Java style (Jsty). If the underlying collection is modified, the iterator becomes invalid and an exception is raised.

An example of code that may generate an exception is below:

try { int sum = 0; for ( int v : data ) { if (v == null) continue; if (v < 7) data.remove(0); // remove the first element // continue looping sum += v; } } catch (NoSuchElementException nsee) { // ... handle as needed } 

There are 4 necessary methods for the Array. get(), set(), remove() and iterator().

There are 2 necessary methods for Iterator. hasNext() and next().

Other than these, you need to create more methods for communicating between Array and the Iterator. Object references are very useful in this scenario.

Once you have an iterator going. There is the next step of handling multiple iterators for the same data structure. More than one iterator can exist, at different times. There are iterators which are

are invalid and have raised an exception, or

are invalid and have not yet raised an exception, or

are valid, they were created after modification to the data structure.

There are two classes gived that we need to complete:

..........................................................................................................

/* * An array that implements the Iterable interface. * Returns an iterator that would raise an exception if the array was modified (set or remove) * */

import java.util.LinkedList;

public class JstyArray implements Iterable { // YOUR CODE ANYWHERE IN THIS CLASS

private E[] array; public static final int CAPACITY = 100; // there could be more instance variables!

/* initialise array with capacity. If capacity is less than 1, use CAPACITY. */ public JstyArray(int capacity) { // YOUR CODE ANYWHERE IN THIS CLASS }

/* if index is outside range of array. simply return */ public void set(int i, E val) { // YOUR CODE ANYWHERE IN THIS CLASS }

/* if index is outside range of array. return null */ public E get(int i) { // YOUR CODE ANYWHERE IN THIS CLASS }

/* if index is outside range of array. return null */ public E remove(int i) { // YOUR CODE ANYWHERE IN THIS CLASS } public JstyIterator iterator() { // YOUR CODE ANYWHERE IN THIS CLASS } }

...........................................................................

/* * An iterator for the class JstyArray. Performs all standard tasks of an iterator. * Specifically, when calling next() on an invalid iterator, a NoSuchElementException is raised by this class. * */ import java.util.Iterator; import java.util.NoSuchElementException;

public class JstyIterator implements Iterator { // YOUR CODE ANYWHERE IN THIS CLASS

public boolean hasNext() { // YOUR CODE ANYWHERE IN THIS CLASS }

public E next() throws NoSuchElementException { // YOUR CODE ANYWHERE IN THIS CLASS } public void remove() { // we need this method for the nagging Java compiler // you may leave empty } }

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!