Due by the end of this meeting
Revise ArrayBag
by implementing the iterator method.
The implementation requires you to make a private class
named BagIterator
that implements Iterator<T>
inside the ArrayBag class.
Do not make the private classstatic. By making it an "instance class" it can refer to elements of the ArrayBag that created it. In particular, it can talk aboutnumInBagandcontentswithout needing to specify which ArrayBag they're part of. They automatically refer to the fields of the correct ArrayBag.Also, because it is an instance class, it has access to the type parameter T, so you don't need to re-introduce that parameter.
A BagIterator is an object that lets a client iterate thru the elements of this Bag. The BagIterator class has the following methods:
next(),
which returns the next item from this Bag
that hasn't already been returned by next().
hasNext(),
which checks whether there are any items in the Bag
that haven't already been returned by the next() method.
A BagIterator is used like any other Iterator:
Run the program TestIterator program to ensure that your iterator loop works. If you've done everything correctly, you should see something like this:
NOTE that it's fine if you see this instead:
next method
so that it throws a NoSuchElementException
if all of there are no more items to return.
Once you have completed the implementation, your output may look like this:
Alternatively, it may look like this:
Submit this/these files: