Deletes one or more consecutive elements in the array.
Refer to the underlying source code implementation of StringBuffer:
Public final class StringBuffer extends actstringbuilder implements java. io. Serializable, CharSequence
char[] value;int count;public AbstractStringBuilder delete(int start, int end) { if (start < 0) throw new StringIndexOutOfBoundsException(start); if (end > count) end = count; if (start > end) throw new StringIndexOutOfBoundsException(); int len = end - start; if (len > 0) { System.arraycopy(value, start+len, value, start, count-end); count -= len; } return this; }
Policy parsing:
Test System. arraycopy () method: String [] array1 = {1, 2, 3, 4, 5}; // System. arraycopy (value, start + len, value, start, count-end); // System. arraycopy (src, srcPos, dest, destPos, length); System. arraycopy (array1, 4, array1, 3, 1); // starting from an element with an index of 4, replace the element with a length of 1 printArray (array1 ); // 1 2 3 5
Analyze count-= len;After the array element is replaced, it becomes 12355, count = number of elements in the source array len = The Last index we want to replace-start Index = (the number of elements to be deleted) count-= len; // indicates that after the array is deleted, the length of len is reduced. Therefore, count-= len; 12355 takes four lengths .. It is 1235. Then, according to the result, 4 has been deleted...
Besides, System. arraycopy (src, srcPos, dest, destPos, length); this method is public static native void arraycopy (Object src, int srcPos, Object dest, int destPos, int length ); now that you see native, you don't have to talk about it more. The bottom layer is C ++. API interpretation is complete.
To delete an element is to move the element behind the element to be deleted to the END-START position. Then you can set the length.