课程: Java Object-Oriented Programming

今天就学习课程吧!

今天就开通帐号,24,600 门业界名师课程任您挑!

Challenge: Reduce redundant code with inheritance

Challenge: Reduce redundant code with inheritance - Java教程

课程: 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…

内容