To please your Project Manager, a former developer (yeaaars ago), you
sometimes let him help you develop some "very important" parts of your
application.
Today, he's in charge of displaying "Hello World" by iterating on a list
containing those words. Alas, distracted by his going on vacation this
very afternoon, he forgets to add "World" to the list before starting
the iteration. Trying to correct his mistake, he adds it a few lines
later, but now his code unexpectedly breaks down at runtime ("this must
be a JVM bug !").
A few minutes before leaving, he asks you to find a solution in his
absence, with the following instructions :
- Do not modify his existing code, it's Perfect (of course).
- The
FIXME
tag shows where you're allowed to insert your corrective code
- He must be able to understand your solution when he comes back (so
using Reflection is not an option).
Are you worth the trust of your beloved Manager ?
final List<String> list = new ArrayList<String>() {{ add("Hello"); }};
final Iterator<String> iterator = list.iterator();
System.out.println(iterator.next());
list.add("World");
// FIXME : work here while I'm sunbathing
System.out.println(iterator.next());
Hints - Keep in mind that :
- the iterator is declared final
- this is only a code fragment, so you cannot use System.exit(0) or return; you wouldn't like your application to terminate prematurely, would you ?
- since you cannot modify the existing code, you cannot delete or ignore the last line, which must print "World"
Note : I must thank Romain Revol for helping me to write this quiz. Romain successfully attended the "Heinz Kabutz's Java Specialist Master Course" training session I presented in France in June at Zenika's office.