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 pointAssignment of a reference variable<Head First>p57.

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<='z'||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: <understanding abnormality> 

gitte address:2022-6-1<a part of String and exception> · 孙雨嘉/Practice the warehouse - 码云 - 开源中国 (gitee.com)

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值