Arrays of Java tools, String class StringBuffer and StringBuilder (Day17)

1. Array Tools

java.util.Arrays

  • Arrays is a utility class.
  • There is a sort () method, it can sort, static method (For ease of use, method tools are generally static, so the use of the class name to call).
  • Arrays.sort();
  • Lookup method requires the use of Arrays API help files, remember rote, with more than a few times to remember. (In IDEA press the Shift key twice, to find the classes, you can find methods in the class)
  • Binary search method:
    Arrays.binarySearch (ARR, x); x is a value to find. -1 element does not exist.

2. Class String (String)

  • String can not be changed once created. Strings are reference data types.
  • In jdk among the strings are stored directly in the "area method" string constant pool "which, because the string used in the actual development too often, in order to perform efficiently, so the way to put a string of a string constants.
  • Splicing operation does not change the string, the string constant pool but generates another String object. String encountered double quotes will generate a String object.
  • If you create a string with new, it will generate an object in the heap memory, but the string constant pool, too.
    *Here Insert Picture Description
    Here Insert Picture Description
  • When defining a String object, using a double equal sign compare two strings are equal reasons
    Here Insert Picture Description
  • When you create a String object using new reasons for using double number less equal in the following figure: So should the equals method coincidence String class overrides the equals method.
    Here Insert Picture Description
  • The reason string for the object's String class is why the "fas" .equals (k)) may be used
  • topic:
  • Converting the byte array in part or all of the elements into a string method
  • byte [] bytes = {97,98,99};
    String S2 = new new String (bytes);. // There two quantities can be written, first, the index start position, and second conversion length
    System .out.println (s2); (default call toString method, String toString class overrides the method the output abc.).
  • char array similar to the above byte array.
  • compareTo method similar to the C language that strcmp () function. (string.h the library function, according to the alphabetical sorting, comparing two strings are equal)
  • contains () method, it is determined whether the character string contains the string in front of the latter, the return value boolean type.
  • endWith ( ". java") method determines whether the end of string .java.
  • equalsIgnoreCase equals ignore case basis.
  • getBytes () method to convert the string into a byte array.
  • the indexOf () method, the index to determine the first occurrence of the substring in the string.
  • lastIndexOf method, the index of the last occurrence.
  • isEmpty () method to determine whether a string is a null string
  • . Analyzing the array length is the length property is determined whether an empty string is a string with a length method.
  • replace () method, replacing the old string in the string with a new string.
  • split () character within the brackets were split into String array.
  • Whether StartWith () determine if a string substring begins
  • substring () Returns a string from the specified start and end positions, the two parameters to be returned if the specified string between two locations, left and right to open and close.
  • toCharArray () char string into the array.
  • the toLowerCase () to convert the string to lowercase
  • trim () method, the removal of an empty character string of the front and rear.
  • String is only one method is static, does not need an object I (valueof method) to convert into a non-character string. Println method calls and the valueof method, they are able to print are strings on the console.

Development, if the need for frequent string concatenation, what problems exist?

Because java strings are immutable, each splice will have a new string. This will take up a lot of memory area method, resulting in a waste of space (string constant value).

  • Then StringBuffer on stage, ha ha,
  • When used, was poured into categories: import java.util.StringBuffer;
  • Create a default initial capacity is byte 16 of [] array, used to hold the string. Want to append the on (), it will automatically expansion, you want to delete the delete method ...
  • StringBuffer optimizations. 16 to resolve the default space, leading to lack of space, problems often expansion. When the specified capacity is created.
  • StringBuffer sb = new StringBuffer(100);

StringBuilder和StringBuffer

The method of StringBuffer are: synchronized keyword modification, represents StringBuffer safe when multi-threaded environment and StringBuilder are not safe.
Here Insert Picture Description

Published 50 original articles · won praise 8 · views 3064

Guess you like

Origin blog.csdn.net/jiahuan_/article/details/105157701