Interpreting a Java Class - Montana Webmaster

Interpreting a Java Class

The Code

class MovieLister…

public Movie[] moviesDirectedBy(String arg) {
List allMovies = finder.findAll();
for (Iterator it = allMovies.iterator(); it.hasNext();) {
Movie movie = (Movie) it.next();
if (!movie.getDirector().equals(arg)) it.remove();
}
return (Movie[]) allMovies.toArray(new Movie[allMovies.size()]);
}

The Questions

Why is the Return Statement Casting the Type Movie[]?

 

Resources

  1. This class came from https://martinfowler.com/articles/injection.html
    1. Note: this article was written in 2004.
    2. Java 1.0 was first released in 1996
    3. The next major release was in Sept 2004 with Java 5 
    4. Some of the questions are related to the fact that the article was written in early versions of Java.
  2. Why is it called a “container”?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.