Below is my learning objective. I got it started, but I don’t really know where to go from here to implement the program in main. I would appreciate any help!
Objective:
- Add an Iterator object to the card collection
- Iterators are added to collections by creating a private inner class.
- You may use any of the inner class types that are appropriate
- Enumerators and Iterators use a large number to determine when the collection changes.
-
Implement the correct methods, interfaces, and extend the appropriate classes for a class consistent with the Java API.
public class CardCollection { private ArrayList<Card> cards; private ArrayList<Note> notes; public CardCollection() { //constructor initializes the two arraylists cards = new ArrayList<Card>(); notes = new ArrayList<Note>(); } private class Card implements Iterable<Card> { //create the inner class public Iterator<Card> iterator() { //create the Iterator for Card return cards.iterator(); } } private class Note implements Iterable<Note> { //create the inner class public Iterator<Note> iterator() { //create the Iterator for Note return notes.iterator(); } } public Card cards() { return new Card(); } public Note notes() { return new Note(); } public void add(Card card) { cards.add(card); } public void add(Note note) { notes.add(note); } }
You have two concepts here that I think you may be mixing up. An object if Iterable if you can iterate over some internal elements.
So if I have a shopping cart with items in it, I can iterate over my groceries.
So in order to use this functionality, I need to provide an Iterator. In your code example, you are reusing the iterators from ArrayList. From your exercise description, I believe you need to implement one yourself. For example:
So I sorta gave you a hint with the constructor/member variables. After you make this class, your Iterable class (my ShoppingCart) will return my new iterator.
The assignment recommends using a private inner class for your custom Iterator.
Good luck