Abnormality and some String practice
First:
Attention:Here STR ="test OK "is not changing the value but making STR refer to a new object
knowlodge point:Assignment of a reference variablep57.
Secondary:
Answe:false
Third:
Knowledge point: charater's ordinary pool
public class String { public static void main(String[] args) { StringBuilder stringBuilder=new StringBuilder(); stringBuilder.append("hello"); stringBuilder.append("world"); java.lang.String s=stringBuilder.toString(); System.out.println(s); }}
knowledge point:
Knowledge point:
The append method always adds these characters to the end of the builder, so the contents of the StringBuilder can be modified
interview question:
1. The difference between String, StringBuffer, StringBuilder
The contents of String cannot be modified. The contents of StringBuffer and StringBuilder can be modified.
A StringBuffer is similar to a StringBuilder in most respects
StringBuffer is a synchronous, thread-safe operation. StringBuilder does not use synchronization, which is a thread-unsafe operation
2.How many String created in total?[Regardless of whether the constant pool existed before]
String str = new String("ab"); // 会创建多少个对象String str = new String("a") + new String("b"); // 会创建多少个对象
Answer:2 and 5
Fourth:
topic:
1.
public class Pratice { public static void main(String[] args) { String word="loveleetcode"; int ret= Find (word); System.out.println(ret); } static int Find(String word){ int[]count=new int[26]; for (int i = 0; i <word.length(); i++) { char ch=word.charAt(i); count[ch-'a']++; } for (int i = 0; i <word.length(); i++) { char ch=word.charAt(i); if (count[i]==1){ return i; } } return -1; } }
2.Calculate the length of the last word in the string
public class Pratice { public static void main(String[] args) { String word="loveleetcode"; int ret= Calculate (word); System.out.println(ret); } static int Calculate(String word) { String s=word.substring(word.lastIndexOf(' ')+1); int len=s.length(); return len; } }
Split by characte:unfamiliar
Train of thought:
Attention:Conversion to lower-case
3.
public class Pratice { public static void main(String[] args) { String word="A man, a plan, a canal: Panama"; word=word.toLowerCase(); boolean ret= Estimate (word); System.out.println(ret); } static boolean Wheather(char ch){ if (ch>='a'&&ch='0'&&ch<='9'){ return true; } return false; } static boolean Estimate(String word) { int left=0; int right=word.length()-1; while (left<right){ while (left<right&&!Wheather(word.charAt(left))){ left++; } while (left<right&&!Wheather(word.charAt(right))){ right--; } if (word.charAt(left)!=word.charAt(right)){ return false; }else { left++; right--; } } return true; } }
Fifth:
gitte address:2022-6-1 · 孙雨嘉/Practice the warehouse - 码云 - 开源中国 (gitee.com)