can anyone throw some light on why is removeRange(int fromIndex, int toIndex)
in Java Vector is protected?
Syntax -
protected synchronized void removeRange(int fromIndex, int toIndex)
I have studies a few blogs and did some code to understand but things are not really clear here. I have looked inside Vector.java and tried to create some understanding as well. But my overall perception is that removeRange(int fromIndex, int toIndex)
will eventually get removed.
I felt sublist(int fromIndex, int toIndex).clear()
is capable of doing the same job. And looks more apt from implementation and usability point of view.
Please help in understanding if you have a better thought.
It is there because it is inherited form
AbstractList.removeRange
. The intention for exposing it in this abstract base class is documented as enabling subclasses to improve the performance ofclear()
:It is extremely unlikely that this method will ever be removed, as this would be a major compatibility problem.
But you are right that as a user of this class you should stick with the public interface, the protected methods are mostly useful for implementing subclasses.
So, why is the method not made public?
The JavaDocs for
List.subList
tell usSo not including range operations in the public interface was a deliberate design choice. On any
List
implementation, these operations can be performed by invoking operations on sublist views.