This article allows reprint, but please indicate the source: http://blog.csdn.net/wanghantong/article/details/46730591, All rights reserved
refer to StringBuffer's underlying source code implementation:
Public Final class StringBuffer extends Abstractstringbuilder implements Java.io.Serializable, Charsequence
<span style= "FONT-SIZE:14PX;" >char[] Value;int count;public abstractstringbuilder Delete (int start, int end) { if (Start < 0) throw new S Tringindexoutofboundsexception (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; } </span>
This article allows reprint, but please indicate the source: http://blog.csdn.net/wanghantong/article/details/46730591, All rights reserved
Policy resolution:
<span style= "FONT-SIZE:14PX;" >test system.arraycopy () method: string[] array1 = {"1", "2", "3", "4", "5"};//system.arraycopy (value, Start+len, Valu E, start, count-end); System.arraycopy (SRC, Srcpos, dest, destpos, length); System.arraycopy (Array1, 4, array1, 3,1);//starting with the element with index 4, replacing the element from the starting index to 3, the replacement length is 1printArray (array1);//1 2 3 5 5</span>
This article allows reprint, but please indicate the source: http://blog.csdn.net/wanghantong/article/details/46730591, All rights reserved
Analyze Count-= len;after the array element is replaced, it becomes 12355,count = number of elements in the source arraylen = The last index we want to replace-index = (number of elements expected to be deleted)count-= len;//indicates that after the array has been deleted, the length is reduced by Len. So count-= Len;12355 take 4 lengths. Is 1235, then look at the result, 4 has been deleted ...
besides system.arraycopy (SRC, Srcpos, dest, destpos, length); This method Public static native void Arraycopy (Object src, int srcpos,Object dest, int destpos,int length);See native, no more talk, and then the bottom is C + +. The API has been read.
Deleting an element is to move the element behind the element that is about to be deleted forward end-start a position. Then take the length to get it.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Delete one or more contiguous elements in an array