In the Book() constructor, your validation probably doesn't behave the way you intended. For each book in the library, you'll want to appends its string representation to the result. findTitles() should return a list of matching books - so where's your new list?Ī hint for Library.toString(): you want to build a long string, so use a StringBuilder object. Returns a properly formatted String representation of all the books in 666 (Don’t let this number concern you :) ), you will use don’t worryĪbout articles such as The or A, etc.). Title field (sort these titles just as they are, i.e. Sorts the library’s book ArrayList in ascending order according to the The String compareTo method is useful here. (exactly) with the passed argument and returns this list to theĬalling program. Generates an ArrayList of all books which have titles that match Public ArrayList findTitles(String title) Operation succeeded) See the add method of ArrayList: Notice it returns a boolean (whether or not the add Otherwise it adds the Book argument to the end of the Public Library( ) Creates an empty ArrayList of books.įirst checks for null or empty Strings and calls the appropriateĮxception. Library’s Book ArrayList should take on all the books in other. Throws a NullPointerException if other is null. This will hold all the Book objects in the library. I can provide the Book class if you need it.įields: A single private ArrayList of Book field is all that is I have the Book class done, but need some help on the Library class. After calling a method that can return null and using its return value a check of the returned value against null is quite common in modern code.I have this Book program that contains 2 classes: Book and Library. In most cases returning the built in null value is expressiveĮnough. This could be achieved through returning null So, lets say you store the Integers 1, 3, 7 in your arraylist at its corresponding index:ĭue to some computation, you want to get the element at index 5, so what your array This implies that null cannot be replaced in any case by a more fluent version of the nothing value. The single most important reason is that null is the do not know equivalent of any reference type, including framework types. This seems to be more of a (software-)philosophic question here.ĪrrayList as a utility class is designed to be helpful in a wide context of possible use-cases.Ĭontrary to your hidden claim that accepting null as a valid value should be discouraged, there are many examples where the null value is perfectly legal. However, this is not the behaviour that most programmers / applications need. You could easily implement that behaviour by creating a wrapper class.
JAVA ARRAY VS ARRAYLIST STACK OVERFLOW CODE
It would have been easier if the ArrayList had thrown an exception in the code where the elements was being added. For instance it might mean that the value at a given position in the list has not been initialized or supplied. Obviously, any case where null has a distinct meaning. Is there any case where I would want to add null to an ArrayList? By contrast, you can prevent an ArrayList from containing nulls by either testing values before adding them or using a wrapper that prevents this happening. The answer to "why" is that if it didn't the ArrayList wouldn't be usable in cases where it is necessary to put a null in the list. Implements all optional list operations, and permits all elements, including null." " resizable-array implementation of the List interface. Side note there is an auxiliary discussion in comments below, along with additional considerations supporting the reasoning laid out here.ĪrrayList allows null by design. If you would want a List implementation disallowing nulls, it would better be called like NonNullableArrayList or something like that, to avoid confusing API users. offers constant-time positional access and is just plain fast. Reasoning like above is presented in JCF tutorial stressing points that suggest close similarity between ArrayList and plain arrays:ĪrrayList. Would be quite surprised to find out if similar code for ArrayList would throw NPE: t(0, null) // NPE => WTF? API user knowing that below works OK: array = null // NPE won't happen here This in particular, involves treatment of null elements. Name ArrayList suggests to reader a functionality similar to arrays - and it is natural for Java Collections Framework designers to expect that vast majority of API users will rely on it functioning similar to arrays. This design decision appears mostly driven by naming.