Agree with Ralph Cook. If you go through the Java™ tutorials section, you will find a Set implementation which records the order of insertion. That might be more appropriate for your requirements.Ralph Cook wrote: . . . Now -- java already has a Set interface, and implementations of it, . . .
. . . and a few lines later. . . lists typically allow duplicate elements.
So your design of class is specifically permitted in the API documentation, where there is a suggestion about how to implement it.It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we expect this usage to be rare.
Campbell Ritchie wrote:There is a rule of thumb: "favour composition over inheritance."
It might be easier for you to create a class which does not extend anything, but has a List as a field. I can imagine it looking like this
This architecture is called a wrapper class.
Agree that is disturbing.Ralph Cook wrote: . . . Now, something else you said is a little disturbing: "I need to keep tracking of the index of those elements..." . . .
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |