From the course: Java Object-Oriented Programming

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Challenge: Reduce redundant code with inheritance

Challenge: Reduce redundant code with inheritance - Java Tutorial

From the course: Java Object-Oriented Programming

Challenge: Reduce redundant code with inheritance

(upbeat chiming music) - [Instructor] One common error when using the ArrayList class is sometimes you might try to retrieve an item that's at an invalid index. To avoid this, usually you'll add an if statement in order to check that the index is valid before retrieving the item from the list. However, sometimes those checks can get annoying and another developer might forget to do the check. To provide a foolproof way of accessing items in a given list, we want to create a ModArrayList class with a getUsingMod method that takes in an index and retrieves an item from the list at that index. If the index is invalid, then the implementation should use the mod operation and mod the index based on the length of the list. If the index is negative, then it should be made positive. If you're unfamiliar with mod, mod is similar to division. However, it returns the remainder rather than the quotient when given two numbers of the same…

Contents