and StringBuffer final modification of several cases (splicing, reverse, symmetry operations)

When the modified final keyword if the basic data types of variables, its value can not be changed once after initialization; if it is a reference type variable, then after its initialization can not let it points to another object, but the reference variables can not be changed, the content object reference variable points in can still change

final static final modification with the difference variables

The final value can be modified to change the content can not be changed reference address. static fina modified initialized because it is static, it has been initialized when loading, rather than each time you create a new object. So the content can not change

While commonly used static final modification of the constants

Several cases StringBuffer

 

1,
  the array into a string of splicing

Copy the code
 1 public class StringBufferTest2 {
 2 
 3 public static void main(String[] args) {
 4 // define an array
 5 int[] arr = {12,21,33,9,2};
 6 
 7 // Mode 1: Define the function, use the String do stitching
 8 String s1 = arrayToString(arr);
 9 System.out.println ( "String splicing method:" + s1);
10 
11 // Mode 2: Define function can use StringBuffer do stitching
12 String s2 = arrayToString2(arr);
13 System.out.println ( "spliced ​​StringBuffer method:" + s2);
14 
15 }
16 
17 public static String arrayToString(int[] arr) {
// define a 18 [String
19 String s = "[";
// array for the traverse 20 and to a string
21 
22 for(int x=0;x<arr.length;x++){
23 if(x == arr.length-1){
24 s += arr[x]+"]";
25 }
26 else{
27 s += arr[x]+",";
28 }
29 }
30 return s;
31 }
32 
33 public static String arrayToString2(int[] arr) {
34 
35 // define a "[" the StringBuffer buffer
36 StringBuffer sb = new StringBuffer();
37 sb.append("[");
38 
39 // traverse the array of conduct, and converted to StringBuffer buffer
40 for(int x=0;x<arr.length;x++){
41 if(x == arr.length-1){
42 sb.append(arr[x]);
43 }
44 else{
45 sb.append(arr[x]).append(",");
46 }
47 }
48 sb.append("]");    
49 // StringBuffer converted into a String
50 return sb.toString();    
51 }
52 }
Copy the code

 

2,
   to reverse a string

Copy the code
 1 import java.util.Scanner;
 2 public class StringBufferTest3 {
 3 
 4 public static void main(String[] args) {
 5 Scanner sc = new Scanner(System.in);
 6 System.out.println ( "Please enter the data you want to reverse");
 7 String str = sc.nextLine();
 8 
 9 // call the method 1 reverse1
10 String result = reverse1(str);
11 System.out.println ( "the inverted data:" + result);
12 
13 // call the method 2 reverse2
14 String result2 = reverse2(str);
15 System.out.println ( "the inverted data:" + result2);
16 }
17 
18 // Mode 1: The String method
19 public static String reverse1(String s){
20 
// define a 21 [String
22 String s1 = "";
23 
// string 24 turn into an array
25 char[] ch = s.toCharArray();
26 
27 // traverse the splicing element as well as an array of strings
28 for(int x = ch.length-1;x >= 0;x--){
29 
30 if(x == 0){
31 s1 += ch[x];    
32 }
33 else{
34 s1 += ch[x];    
35 }
36 }
37 return s1;
38 }
39 
40 // Embodiment 2, the inversion method using StringBuffer 
41 public static String reverse2 (String s){
42 
43 // convert String to StringBuffer type of input type
44 //StringBuffer buffer = new StringBuffer(s);
45 
46 // StringBuffer inverted function of:
47 //public StringBuffer reverse()
48 //return buffer.reverse().toString();
49 
50 // concise edition
51 return new StringBuffer(s).reverse().toString();
52 }
53 
54 }
Copy the code

 

3,
  determines whether a string the string is symmetrical
    Analysis:
      two ways to accomplish:
      A: string accomplished by:
        first string into an array, then the data on the inside, one by one comparison, the first and last a, and a second inverted two ....
      B: StringBuffer accomplished by:
        a direct comparison to the original value of inverted
      C: creation method, respectively, must confirm two values:
                a: return type: Boolean
                B: list of parameters: String 

Copy the code
 1 import java.util.Scanner;
 2 public class StringBufferTest4 {
 3 
 4 public static void main(String[] args) {
 5 // Create keyboard input
 6 Scanner sc = new Scanner(System.in);
 7 System.out.println ( "Please enter the string you want to judge:");
 8 String str = sc.nextLine();
 9 
10 // call the first method
11 boolean b1 = symmetry1(str);
12 System.out.println ( "string is symmetrical:" + b1);
13 
The first method calls 14 // 2
15 boolean b2 = symmetry2(str);
16 System.out.println ( "string is symmetrical:" + b2);
17 
18 // call the second method
19 boolean b3 = symmetry2 (str);
20 System.out.println ( "string is symmetrical:" + b3);
21 } 
22 
23 @ Embodiment 1: a method to a string comparison
24 public static boolean symmetry1(String s){
25 // string into an array
26 char[] c = s.toCharArray();
27 // compared, define two variables: start and End, start from both ends Comparative
28 for(int start = 0, end = c.length-1;start < end ; start ++,end--){
29 if (c [start]! = C [end]) {// for Analyzing
30 return false;
31 
32 }    
33 }
34 return true;
35 }
36 
Embodiment 1 37 // another way:
38 public static boolean symmetry2(String s){
39 // define a boolean value
40 boolean flog = true;
41 
42 // string into an array
43 char[] c = s.toCharArray();
44 
45 // compared, define two variables: start and End, start from both ends Comparative
46 for(int start = 0, end = c.length-1;start < end ; start ++,end--){
47 if (c [start]! = C [end]) {// for Analyzing
48 flog = false;
49 break;
50 }    
51 }
52 return flog;
53 }
54 
55 // 2 way use StringBuffer reverse function
56 public static boolean symmetry3(String s){
57 return new StringBuffer(s).reverse().toString().equals(s);
58 // start becomes StringBuffer String type, then it is inverted, and then changed back to a string type, let whether a string comparison of the original data obtained at this time inside the same
59 }
60 }
Copy the code

Guess you like

Origin www.cnblogs.com/h-c-g/p/11081425.html
Recommended