Java implemented in several operations on the string inversion (CharAt6, append1, reverse3, France) (user input from the keyboard 999 determines a palindromic 1) (brief description of the difference between the StringBuffer and String)

1.①String class is immutable class, StringBuffer \ StringBuilder variable class
 Examples

String a = "123";  //1
a = "456";         //2

The above code, is a seemingly been modified from the original "123" to "456", it is not really,
in line 2, to carry out a "assignment" operation, in fact, create a new String object,
and a point the newly created object, the original object will still exist until being recovered.

② The StringBuilder \ StringBuffer, is a variable type
that is, they were the subject of a string variable, it does not regenerate a target, but the new connection string on the basis of the original object.

butIf the add operation

String x ="abc";
x+="defg";

Formally equivalent to the following

StringBuffer a = new StringBuffer("abc"); 
                    //创建一个StringBuffer类的对象a
a.append("defg");   //给字符串a的结尾追加字符串"defg"
x = a.toString();   //将a以String形式赋给x

StringBuffer string modification is to first create a StringBuffer, StringBuffer's append method followed by the call, the last call of toString StringBuffer () method returns the results

Involves a knowledge append method
              Code

          x.append(str);  //给字符串a追加子串str

On the Add String operation, String performance is less than StringBuffer, if such an operation more words (such as recycling) is recommended to use StringBuffer.

StringBuilder StringBuffer stronger performance ratio, but not enough thread safety

--------------Dividing line---------------

2.StringBuilder and StringBuffer very similar
but because all methods of StringBuffer has been added to the synchronized keyword, this method is to add a lock to guarantee thread safety.

StringBuffer so slightly slower, but thread safety.

But not the above-described keyword StringBuilder, so performance slightly.

--------------Dividing line---------------

3. With regard to reverse both contained () function exemplary method of reversing the string and explanations
 
 Examples

public class Test{
public static void main(String args[]){
String ans = "Mustang";
String reverse_ans = new StringBuffer(ans).reverse().toString();//1
System.out.println(reverse_ans);  
//最终输出gnatsuM 
}
}

At line 1 toString () is the output StringBuffer String class, is returned to the reverse_ans;

--------------Dividing line---------------

4. With regard to first string into char string type array inversion method uses the String toCharArray method
implemented in the form of questions do.

Problem:
the user keyboard input from a number between 1 to 9999, determines the number is few, and determines whether the number is a palindrome. Palindrome refers to the number and the same number of the original digital number contained in the reverse order to obtain, for example, are 12121,3223 palindrome. 
             Code

import java.util.*;
   public class Test{
   public static void main(String args[]){
   Scanner reader = new Scanner(System.in);
   String x = reader.nextLine();
   char []arr = x.toCharArray(); 
   String ans = "";
   for(int i =arr.length-1;i>=0;i--)
   ans = ans+arr[i];
   if(ans.equals(x))  //1111111111  
   System.out.println("是回文数");
   else
   System.out.println("不是回文数");
}
}

Analyzing ans and x are equal at the annotation process is

                         a.equals(b);

Instead of

 a==b  //错!

Since a and b are of type String string, if the string is == Analyzing two are not equal, but the determination whether or not they are equal reference. this is not right.
They want to determine whether the contents are equal, should be judged by a.equals (b).

--------------Dividing line---------------

5. Use toCharArray () method of the String to string into char array after inversion

String a = "123456";
char [] x = a.toCharArray();
String ans = "";
for(i=x.length;i>=0;i--)
ans = ans+x[i];
System.out.println(ans);//得到反转字符串654321

--------------Dividing line---------------

6. Use of StringCharAtMethod, taken out one by one additional string

public class Test{  
public static void main(String args[]) {
    String reverse = "";
    String str = "abc";
    int length = str.length();
    for (int i = length-1; i >= 0; i--) {
      reverse = str.charAt(i) + reverse;
    }
    System.out.println(str);
  }
}
Published 32 original articles · won praise 6 · views 1534

Guess you like

Origin blog.csdn.net/Pan_ZC/article/details/104964299