StringBuffer, advanced arrays and wrapper classes

Source: Internet
Author: User
Tags wrapper

1:stringbuffer (Master)
(1) concatenation with strings, time consuming and also consumes memory, and this splicing operation is more common,

to solve this problem, Java provides a string buffer class.

StringBuffer for our use.

(2) The construction method of StringBuffer

StringBuffer () stringbuffer (int  size) stringbuffer (String str)

(3) Common features of StringBuffer

    • Add function sb.append ("1111111111");
    • Delete function sb.delete (6, 10);
    • Replacement function sb.replace (2, 4, "Oh");
    • inversion function sb.reverse ();
    • Intercept function (Note this return value)

(4) StringBuffer exercises ( Do it again)
a:string and StringBuffer convert each other
string--StringBuffer
        construction method
stringbuffer--String
        ToString () method
b: Concatenation of strings
c : Reverses the string
d: Determines whether a string is symmetric
(5) Small details:

The difference between String,stringbuffer,stringbuilder?

  A:string are immutable, and strignbuffer,stringbuilder are content-changeable.

  B:stringbuffer is synchronous, the data is safe, the efficiency is low, the StringBuilder is not synchronized, the data is unsafe, and the efficiency is high.

What is the difference between a stringbuffer and an array?

  Both can be thought of as a container, loaded with other data.

However, the StringBuffer data is ultimately a string data.

< EM id= "__mcedel" >

< EM id= "__mcedel" > formal parameter problem:

Basic type: Changes in formal parameters do not affect actual parameters
Reference type: Changes in formal parameters directly affect actual parameters

Note: string is passed as a parameter, and the effect and the base type are passed as arguments.


2: Array advanced and Arrays (master)
(1) Sort
  Bubble sort
Adjacent elements 22 comparison, large back, the first time, the maximum value appears at the maximum index.

In the same vein, other elements can be lined up.

 Public Static voidBubblesort (int[] arr) {         for(inti = 0; i < arr.length-1; i++) {             for(intj = 0; J < arr.length-i-1; J + +) {                if(Arr[j] > arr[j + 1]) {                    inttemp =Arr[j]; ARR[J]= Arr[j + 1]; Arr[j+ 1] =temp; }}} System.out.print ("After sorting:"); }

  Select sort
The elements of the 0 index, and the elements after index 1 are compared, the first is complete, the minimum value appears in the 0 index.

In the same vein, other elements can be lined up.

 Public Static voidSelectarray (int[] arr) {         for(inti = 0; i < arr.length-1; i++) {             for(intj = i + 1; J < Arr.length; J + +) {                if(Arr[j] >Arr[i]) {                    inttemp =Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; }}} System.out.print ("After sorting:"); }

(2) Find
  Basic find
In case of an unordered array

 public  static  int  getindex (int  [] arr,int   value) { int  index =-1    ;  for  (int  x=0; x<arr.length; x++< Span style= "color: #000000") { if  (arr[x] == value) {index  = x;         break  ;                    }}  return   index;} 

  Binary lookup (binary lookup)

For an orderly array of conditions (never sort first, find)

 Public Static intGetIndex (int[] arr,intvalue) {        intmax = Arr.length-1; intMin = 0; intMid = (max + min)/2;  while(Arr[mid]! =value) {            if(Arr[mid] >value) {Max= Mid-1; } Else if(Arr[mid] <value) {min= Mid + 1; }            if(Max <min) {                return-1; } Mid= (max + min)/2; }        returnmid; }

(3) Arrays Tool Class
is a tool class that operates on an array. Includes functions such as sorting and finding.

 public  static  String toString (int  [] a) //  array Turn to string  public  static  void  sort (int  [] a) //  sort the array (bottom using quick sort)  public  static  int  BinarySearch (int  [] a,int  key) //   binary find  

(4) Sort the characters in the string
Example:
"EDACBGF"
Get results
"ABCDEFG"

3:integer (Master)
(1) Packing type

To make the basic type of data more operational, Java provides the corresponding wrapper class type for each base type.

(2) How to construct integer

New Integer ( "100");

Note: The string must be made up of numeric characters   
(3) mutual conversion of string and int
String --INT
Integer. parseint ("+");
int --String
String. valueOf (a);
(4) Other functions (Learn)
Binary Conversion
(5) new features of JDK5
Auto-boxing base type--reference type  
Auto-unpacking reference type-Basic type  
The following code is understood:

    Integer i = +;     + = 200;

(6)-Data buffer pool issues between 128 and 127

  Note: The integer data is directly assigned, and if it is between -127~128, the data is fetched directly from the buffer pool.

4:character (Learn)
(1) Character Construction method

New Character (' a ');

(2) The method to be mastered:

    • Determines whether the given character is uppercase
    • Determines whether the given character is lowercase
    • Determines whether a given character is a numeric character
    • Turn a given character into uppercase
    • Turn a given character into lowercase

(3) Case:
Number of occurrences of uppercase, lowercase, and numeric characters in the statistics string

StringBuffer, advanced arrays and wrapper classes

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.