The Methods getIndexOf and contains
In this lesson, we will define the methods getIndexOf and contains so that they do not duplicate the search for a given string.
We'll cover the following...
Having just defined the private method removeEntry in the previous lesson, we now need to locate the string to remove from the bag so we can pass its index to removeEntry. That is, we must define the private method getIndexOf.
Locating the string to remove: The dilemma
We want the private method getIndexOf to search the array of bag entries for a given string and to return the string’s index if it is found. But the method contains already does the same search. Unfortunately, contains returns true or false; it does not return the index of the string it locates in the array. Thus, getIndexOf cannot simply call contains.
📝 Design decision: Should the method
containsreturn the index ...