The article "arraylist original code analysis" mentions "Why does removerange (INT fromindex, int toindex) be protected ?"
First, the source code of the removerange (INT fromindex, int toindex) method is provided.CodeI will not explain what it is, as described in the source code analysis)
1 Protected Void Removerange ( Int Fromindex, Int Toindex ){ 2 Modcount ++ ; 3 Int Nummoved = size- Toindex; 4 System. arraycopy (elementdata, toindex, elementdata, fromindex, 5 Nummoved ); 6 7 // Let GC do its work 8 Int Newsize = size-(toindex- Fromindex ); 9 While (Size! = Newsize) 10 Elementdata [-- size] = Null ; 11 }
You can see that the removerange method "deletes" all elements in the specified range. Why is this method not exposed to users?
Internet query part of the information, a little reliable explanation: http://stackoverflow.com/questions/2289183/why-is-javas-abstractlists-removerange-method-protected
Verify the code and execution result with examples:
1 Public Static Void Main (string [] ARGs ){ 2 Arraylist <integer> ints = New Arraylist <integer> (arrays. aslist (0, 1, 2 , 3 3, 4, 5, 6 )); 4 // Fromindex low endpoint (inclusive) of the sublist 5 // Toindex high endpoint (exclusive) of the sublist 6 Ints. sublist (2, 4 ). Clear (); 7 System. Out. println (ints ); 8 }
The running result is:[0, 1, 4, 5, 6]
Have you found this result is like calling removerange (2, 4 )! What's going on? Next look!
Because arraylist does not implement the sublist (INT fromindex, int toindex) method, the method of the parent class is called. See the sublist method of the parent class abstractlist:
1 Public List
sublist (
int fromindex,
int
toindex) {
2
return (
This
I Nstanceof randomaccess?
3
New randomaccesssublist
(
This
, fromindex, toindex):
4
New sublist
(
This
, fromindex, toindex);
5 }
Arraylist implements the randomaccess interface (see arraylist original code analysis), so randaccesssublist <E> (this, fromindex, toindex) is returned ). This, fromindex, and toindex can be ints, 2, and 4 in the preceding example.
The following is the source code of randaccesssublist <E> (this, fromindex, toindex.
1 ClassRandomaccesssublist <E>ExtendsSublist <E>ImplementsRandomaccess {2Randomaccesssublist (abstractlist <E> list,IntFromindex,IntToindex ){3Super(List, fromindex, toindex );4}
Only the constructor of the parent class is called. The following shows the called constructor.
1 Sublist (effecactlist <E> list, Int Fromindex, Int Toindex ){ 2 If (Fromindex <0 ) 3 Throw New Indexoutofboundsexception ("fromindex =" + Fromindex ); 4 If (Toindex>List. Size ()) 5 Throw New Indexoutofboundsexception ("toindex =" + Toindex ); 6 If (Fromindex> Toindex) 7 Throw New Illegalargumentexception ("fromindex (" + fromindex + 8 ")> Toindex (" + toindex + ")" ); 9 L = List; 10 Offset = Fromindex; 11 Size = toindex- Fromindex; 12 Expectedmodcount = L. modcount; 13 }
So far, the sublist method call has ended. Next, let's look at the clear () method. Because the sublist method returns list <E>, the clear method calls the clear () method of the javasactlist class.
1Public VoidClear (){2Removerange (0, Size ());3}
I finally saw removerange and hope.
1 protected void removerange ( int fromindex, int toindex) { 2 checkforcomodification (); 3 L. removerange (fromindex + offset, toindex + offset); 4 expectedmodcount = L. modcount; 5 size-= (toindex- fromindex); 6 modcount ++ ; 7 }
This is the called removerange method. If not, L. removerange (fromindex + offset, toindex + offset) is executed. Do you know who l is? L defined in sublist. Do you still remember the list passed in by using sublist? Do you still remember this introduced when sublist is called? Do you still remember Xia Yuhe from dameng Lake ?!!! I ran the question ~~~
Now the removerange (INT fromindex, int toindex) method of arraylist is entered. After a big bend, I finally called this method, but I had another question: Didn't I call sublist through the returned list? <E> did I call clear? After careful observation, we will find that the original arraylist is stored in the sublist constructor. Therefore, when removerange is called, the original list is processed !!!
In combination with the English explanation, in order to avoid redundancy... so it is not open to users, of course you can inherit the arraylist to compile your own list class to call this method.
Finally, the truth is clear. Yes, there will always be one truth!