Finding an object on the list
A method can also go through the list to find one object and return it: the largest, or the first one that matches. Its return type is then the class of the objects. When there's nothing to return, it returns null, "no object" (module 22):
public class Main {
public static void main(String[] args) {
Bookshelf shelf = new Bookshelf("Study");
shelf.add(new Book("Emma", "Austen", 474));
shelf.add(new Book("Kindred", "Butler", 264));
shelf.add(new Book("Middlemarch", "Eliot", 880));
System.out.println("Thickest: " + shelf.thickest());
Book found = shelf.find("Kindred");
if (found != null) {
System.out.println("Found it, by " + found.getAuthor());
}
if (shelf.find("Ulysses") == null) {
System.out.println("No Ulysses here");
}
Bookshelf hall = new Bookshelf("Hallway");
System.out.println(hall.thickest());
}
}import java.util.ArrayList;
public class Bookshelf {
private String room;
private ArrayList<Book> books;
public Bookshelf(String room) {
this.room = room;
this.books = new ArrayList<>();
}
public void add(Book book) {
this.books.add(book);
}
public Book thickest() {
if (this.books.isEmpty()) {
return null;
}
Book thickest = this.books.get(0);
for (Book book : this.books) {
if (book.getPages() > thickest.getPages()) {
thickest = book;
}
}
return thickest;
}
public Book find(String title) {
for (Book book : this.books) {
if (book.getTitle().equals(title)) {
return book;
}
}
return null;
}
}public class Book {
private String title;
private String author;
private int pages;
public Book(String title, String author, int pages) {
this.title = title;
this.author = author;
this.pages = pages;
}
public String getTitle() {
return this.title;
}
public String getAuthor() {
return this.author;
}
public int getPages() {
return this.pages;
}
public String toString() {
return this.title + " by " + this.author + ", " + this.pages + " pages";
}
}Thickest: Middlemarch by Eliot, 880 pages
Found it, by Butler
No Ulysses here
nullthickestkeeps the thickest book so far in a local variable of type Book, starting with the first book on the list. Each book with more pages takes its place. This is module 13's search for the largest value, with objects: the pages decide, but the whole Book is kept.- Starting from
this.books.get(0)needs a first book, so the empty check comes first. Without it,get(0)on an empty list would crash with an IndexOutOfBoundsException. - With
>, a book takes the kept one's place only when it's thicker, so of two equally thick books, the first one wins. With>=, the last one would. findreturns the book as soon as it finds it, which also ends the loop.return nullafter the loop runs only when no book matched.
Checking the result
A method that may return null puts a job on its caller: check before using the result. found.getAuthor() would crash with a NullPointerException if found were null, so main checks found != null first. Printing a null is fine, though: System.out.println(hall.thickest()) prints null.
The method returns the object itself, not a copy. shelf.find("Emma") == shelf.find("Emma") is true: both calls return a reference to the one Book on the shelf. So if Book could change, a change made through the returned reference would change the book on the shelf.