Deletes one or more consecutive elements in the array.

Source: Internet
Author: User

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.

 

 

Related Article

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.