Use the Iterator Pattern: Create a class called Exercise, and then, in its main method, create an ArrayList called stringArrayList. Add the following five strings to stringArrayList: “one”, “two”, ”three”, ”four”, ”five”. Then, use a for loop and the index of stringArrayList to print all these five strings out to the console. 2. For stringArrayList above, can you use an iterator to traverse the five strings inside, without using the index? 3. If change stringArrayList into a TreeSet, how do you traverse the five strings use an iterator? Implement an Iterator: 4. Create a class called StringArray that is able to store a number of String objects. Create an instance variable, String[] values, (internal data storage) of StringArray to store all the strings. 5. Create a constructor for StringArray, which is able to build an object of StringArray using the parameter. public StringArray(String[] values) 6. Make the class StringArray implements Iterable So that it can generate iterators. What function do you need to implement this interface? What should be returned by that function? 7. Create an inner class called ArrayIterator in StringArray. This is our first iterator. Create an int variable called current inside it works as an index for our iterator. 8. Make ArrayIterator implements Iterator interface, so that it becomes an iterator. What functions do you need to implement this interface? Implement these functions. 9. Go back to your StringArray class, what should be returned by the iterator() function? 10. So far, an iterator is done. Create a main method, and build an object of StringArray, add five strings in step 1 to it. Traverse and print them out. 11. Create another iterator (inner class) This new iterator traverses only String elements equal or longer than 4 characters.
Use the Iterator Pattern:
- Create a class called Exercise, and then, in its main method, create an ArrayList called stringArrayList. Add the following five strings to stringArrayList:
“one”, “two”, ”three”, ”four”, ”five”.
Then, use a for loop and the index of stringArrayList to print all these five strings out to the console.
2. For stringArrayList above, can you use an iterator to traverse the five strings inside, without using the index?
3. If change stringArrayList into a TreeSet, how do you traverse the five strings use an iterator?
Implement an Iterator:
4. Create a class called StringArray that is able to store a number of String objects. Create an instance variable, String[] values, (internal data storage) of StringArray to store all the strings.
5. Create a constructor for StringArray, which is able to build an object of StringArray using the parameter.
public StringArray(String[] values)
6. Make the class StringArray implements Iterable So that it can generate iterators. What function do you need to implement this interface? What should be returned by that function?
7. Create an inner class called ArrayIterator in StringArray. This is our first iterator. Create an int variable called current inside it works as an index for our iterator.
8. Make ArrayIterator implements Iterator interface, so that it becomes an iterator. What functions do you need to implement this interface? Implement these functions.
9. Go back to your StringArray class, what should be returned by the iterator() function?
10. So far, an iterator is done. Create a main method, and build an object of StringArray, add five strings in step 1 to it. Traverse and print them out.
11. Create another iterator (inner class) This new iterator traverses only String elements equal or longer than 4 characters.
Step by step
Solved in 2 steps