Append of string strings, copy of array

Source: Internet
Author: User

 PackageChengbaodemo;Importjava.util.Arrays;/*** Requirements: expansion of arrays and copying of data * Analysis: Because the nature of the string is stored in a character array, the append,<br> * of the string is essentially the expansion of the array, the movement of the data (copy) **/ Public classteststring { Public Static voidMain (string[] args) {String src=NewString ("src"); String app=NewString ("App"); String newstring=copy (src, app);    System.out.println (newstring); }     Public Staticstring Copy (String src, string app) {CharSrcarray[] =Src.tochararray (); /*(1) Create a new character array whose size is the length of the original string + the length of the appended string and copies the original string into the new array*/
This method is a static method of the arrays class Char[] buf = arrays.copyof (Srcarray, src.length () +app.length ()); //(2) Copy the character appended to the string into the new character array, note: Both the source object and the destination object are character arrays
This method is the system
System.arraycopy (App.tochararray (), 0, buf, Src.length (), app.length ()); //(3) return a new string return NewString (BUF); }}


Note: The string class is final, is non-inheritable, and cannot be changed;
So the pursuit of the string, the modification is not done in the original string modification, but instead of creating a new string,
An array of strings that speak the original string, and append string data, copied to the line,
The essence: is the expansion of the array, the movement of data
Methods such as the following string classes
public String substring( int beginIndex, int endIndex) {
     if (beginIndex < 0 ) {
         throw new StringIndexOutOfBoundsException(beginIndex);
     }
     if (endIndex > count) {
         throw new StringIndexOutOfBoundsException(endIndex);
     }
     if (beginIndex > endIndex) {
         throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
     }
     return ((beginIndex == 0 ) && (endIndex == count)) ? this :
         new String(offset + beginIndex, endIndex - beginIndex, value);
     }
 
  public String concat(String str) {
     int otherLen = str.length();
     if (otherLen == 0 ) {
         return this ;
     }
     char buf[] = new char [count + otherLen];
     getChars( 0 , count, buf, 0 );
     str.getChars( 0 , otherLen, buf, count);
     return new String( 0 , count + otherLen, buf);
     }
  public String replace( char oldChar, char newChar) {
     if (oldChar != newChar) {
         int len = count;
         int i = - 1 ;
         char [] val = value; /* avoid getfield opcode */
         int off = offset;   /* avoid getfield opcode */
 
         while (++i < len) {
         if (val[off + i] == oldChar) {
             break ;
         }
         }
         if (i < len) {
         char buf[] = new char [len];
         for ( int j = 0 ; j < i ; j++) {
             buf[j] = val[off+j];
         }
         while (i < len) {
             char c = val[off + i];
             buf[i] = (c == oldChar) ? newChar : c;
             i++;
         }
         return new String( 0 , len, buf);
         }
     }
     return this ;


Conclusion:
As can be seen from the above three methods, whether it is a sub, concat or replace operation
is not on the original string, but instead regenerates a new string object.
This means that the most primitive string has not been changed after these operations.

Append of string strings, copy of 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.