Example. Lazy Array Implementation of GuestList (generic version)
[comp1110-4C]
Example. Lazy Array Implementation of GuestList (generic version)
[comp1110-4C]Updating ArrayGuestList to match the generic interface requires adding the type parameter <T> to the class declaration and replacing Object with T in the method signatures. The internal storage remains Object[], however, due to type erasure:
Type erasure is also the reason why the coming method needs an additional argument.
Because we are expected to return an array of T, but still cannot create one, we need to be given an object that can create such an array for us, given a size.
Function is a generic interface with one method (called apply) that accepts two type arguments - the first one is the argument type of apply, the second is the return type.
In this case, any object satisfying Function<Integer, T[] has an apply method that takes an integer (meant to be the intended size of the array) and returns an array (hopefully of the given size).
A user of coming can of course pass any object satisfying the interface, but for any concrete array type, say, String[], we can simply write:
This special notation refers to the constructors of a given type, which for arrays always take an integer for the size of the array. We may talk about why this works at the end of the semester.