Suppose myRoster is an instance of the iterable class Roster. Then we can use enhanced for loop as follows. Fill in the
Posted: Fri May 20, 2022 5:07 pm
Suppose myRoster is an instance of the iterable class Roster.
Then we can use enhanced for loop as follows. Fill in the blanks
(SECOND SCREENSHOT).
Enhanced for loop requires some effort. We have to do some work (making class iterable) before you can use it. Suppose we have the following collection class Roster that contains members of class Student. public class Roster { private int maxEnrolled; private int maxWaitlist; private int totalwithhope; private TreeSet<Student> enrolled; private ArrayList<Student> waitList; private HashSet<Student> hoping; } To make class Roster to be iterable, we can add an inner class Rosterlterator that implements Iterator <Student>. Give the names of the two methods that we have to have to implement interface Iterator <Student> as shown in RED below. B public class Roster implements Iterable<Student> { public Iterator<Student> iterator() { return new RosterIterator; } private class RosterIterator implements Iterator<Student> { public boolean A___O { }; public Student _B___O {...}; } }
(Cont.) Suppose myRoster is an instance of the iterable class Roster. Then we can use enhanced for loop as follows. for (Student s: myRoster) { System.out.println(s); } The reason why the enhanced for-loop works is that Java compiler will (internally) translate the loop as follows. Fill in the blanks. . B = . = Iterator<Student> it = myRoster.-__A___(); while (it. __B___() { Student s = it. _C___O; System.out.println(s); }
Then we can use enhanced for loop as follows. Fill in the blanks
(SECOND SCREENSHOT).
Enhanced for loop requires some effort. We have to do some work (making class iterable) before you can use it. Suppose we have the following collection class Roster that contains members of class Student. public class Roster { private int maxEnrolled; private int maxWaitlist; private int totalwithhope; private TreeSet<Student> enrolled; private ArrayList<Student> waitList; private HashSet<Student> hoping; } To make class Roster to be iterable, we can add an inner class Rosterlterator that implements Iterator <Student>. Give the names of the two methods that we have to have to implement interface Iterator <Student> as shown in RED below. B public class Roster implements Iterable<Student> { public Iterator<Student> iterator() { return new RosterIterator; } private class RosterIterator implements Iterator<Student> { public boolean A___O { }; public Student _B___O {...}; } }
(Cont.) Suppose myRoster is an instance of the iterable class Roster. Then we can use enhanced for loop as follows. for (Student s: myRoster) { System.out.println(s); } The reason why the enhanced for-loop works is that Java compiler will (internally) translate the loop as follows. Fill in the blanks. . B = . = Iterator<Student> it = myRoster.-__A___(); while (it. __B___() { Student s = it. _C___O; System.out.println(s); }