Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Eric Redmond ,Jim Wilson

1st Edition

1934356921, 978-1934356920

More Books

Students also viewed these Databases questions

Question

(2) The function f (x) = sin(x) is a probability density function.

Answered: 1 week ago