StringBuffer
StringBuffer: Thread-safe mutable sequence of characters
StringBuffer constructor :
StringBuffer(): The form of no parameter construction , the initial capacity is 16
StringBuffer(int capacity ) : Construct a string buffer with the specified capacity
StringBuffer(String str) : Constructs a string buffer and initializes its contents to the specified string contents
The acquisition function of StringBuffer :
public int length() : returns the length
public int capacity() : Returns the current capacity ( if the capacity is exceeded , the system will automatically allocate it ( when storing strings , in English ))
Added features of StringBuffer :
( More used in actual development ): public StringBuffer append(String/boolean....) : Append data to the string buffer ( append at the end ), and return the string buffer itself
public StringBuffer insert(int offset, String str): Add the current str string to the specified position , it returns the string buffer itself
The reverse function of StringBuffer :
public StringBuffer reverse() : Reverse the sequence of characters in the buffer and return it ( string ) itself
StringBuffer interception function :
public String substring(int start): intercept from the specified position , intercept to the end by default , the return value is not the buffer itself , but a new string
public String substring(int start, int end) : intercept from the specified position to the specified position , the return value is not the buffer itself , but a new string
Replacement function for StringBuffer :
public StringBuffer replace(int start,int end,String str)
From the specified position to the end of the specified position , replace it with a new str string , and the return value is the string buffer itself
Packaging class
Features after jdk5 : automatic unpacking
The Integer class is a wrapper class type of the int type
Get the maximum value of Integer :
public static final int MAX_VALUE returns the maximum value
public static final int MIN_VALUE returns the minimum value
Integer provides static functions :
public static String toBinaryString(int i) converts the decimal number to binary
public static String toOctalString(int i) converts the decimal number to octal
public static String toHexString(int i) converts the decimal number to hexadecimal
There is a guaranteed type corresponding to the basic type , the purpose is to convert the basic data type to the String type.
byte Byte
short Short
int Integer
long Long
float Float
double Double
char character
boolean Boolean
Constructor of Integer :
public Integer(int value)
public Integer(String s)
Convert int to String type :
valueOf(int i) ;
Convert String to int type :
static int parseInt(String s);
//int---->String
// define a variable of type int
int num = 100 ;
// Method 1: String concatenation
String str = "" + num ;
System.out.println("str:"+str);
// Method 2:valueOf(int i);
//int -->Integer--->String
Integer i = Integer.valueOf(num) ;
String str2 = i.toString() ;
System.out.println("str2:"+str2);
// Method 3: static toString(int i): static String toString(int i )
String str3 = Integer.toString(num) ;
System.out.println("str3:"+str3);
System.out.println("----------------------");
//String--->int
String s = "100";
// Method 1: String-->integer--->int
Integer ii = new Integer(s);
//public int intValue()
int num2 = ii.intValue() ;
System.out.println("num2:"+num2);
// Method 2: ( commonly used ) public static int parseInt(String s)
int num3 = Integer.parseInt(s) ;
System.out.println("num3:"+num3);
Automatic unboxing is provided after JDK5
Integer --> unboxing int type can be
The int type can be boxed ---> the Integer type
Character class :
The Character class wraps a value of the primitive type char in an object . An object of type Character contains a single field of type char .
to determine the class of characters (lowercase letters, numbers, etc.)
Constructor :
public Character(char value)
The judgment function of the Character class :
public static boolean isDigit(char ch) Determines whether the specified character is a digit.
public static boolean isLowerCase(int ch): Determine if it is a lowercase character
public static boolean isUpperCase(int ch): Determines whether to uppercase alphabetic characters
Two conversion functions :
public static int toLowerCase(int codePoint)
public static int toUpperCase(int codePoint)
sort
Bubble sort : two-by-two comparisons , the larger ones are placed later , and the maximum value appears at the largest index after the first comparison. …
for (int x = 0; x < arr.length - 1; x++) {
for (int y = 0; y < arr.length - 1 - x; y++) {
if (arr[y] > arr[y + 1]) {
int temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = temp;
}
}
}
Selection sort : Use the elements corresponding to the 0 index to compare the elements corresponding to the following indexes in turn , 1 index ..... small data forward method , you can get a sorted array ...
for (int x = 0; x < arr.length - 1; x++) {
for (int y = x + 1; y < arr.length; y++) {
if (arr[x] > arr[y]) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
find
Unordered elements in an array : basic search method
Elements in an array are ordered : binary search
Half search ( binary search ): The array must be ordered
Analysis :
A: Define the minimum index and maximum index
B: Calculate the intermediate index
C: Compare the element corresponding to the intermediate index with the element to be searched
Equal , return the intermediate index directly
Not equal :
big , look on the left
max = mid -1 ;
Small , look on the right
min = mid + 1 ;
E: Recalculate the intermediate index and return to B to continue searching