Question: ResizableArray array = new ResizableArray(20); // add some String objects to the array object iterator i = array.iterator(); while (i.hasNext() { System.out.println(i.next()); } Please java

 ResizableArray array = new ResizableArray(20); // add some String objects to

ResizableArray array = new ResizableArray(20);

// add some String objects to the array object

iterator i = array.iterator();

while (i.hasNext() {

System.out.println(i.next());

}

Please java code only.

16.7 Iterating the resizable array With an array, one can start a for-each loop. With this class, that is not possible. We will make it possible in a certain way as well. Looping through an array-like structure is called iterating. First define an interface called Iterator. It is generic, so Iterator. The interface only defines two methods: hasNext() and next()). The first one returns true if there is another element to be gotten, the second one returns the next object if there is one, throwing some exception if not. Create a class ResizableArraylterator, implementing the interface. Make it an inner class to ResizableArray; this is very important, as it spares moving around parameters. In the constructor, initialize a counter to be set at zero. Warning: as the interface is generic, so is the implementing class! The method hasNext() returns true if the counter still points to a slot in the array inside ResizableArray that has an object assigned to it. The method next() will return that object, then move the counter up by one. Give the class ResizableArray an extra method called iterator(). It will return an instance of the (inner) class implementing Iterator. Make the signature Iterator iterator(). Write some test code. It should be possible to have code such as

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!