Delete one or more contiguous elements in an array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.